Jump to content

[SOLVED] Incrementing not by 1


carleihar

Recommended Posts

I'm trying to create a program that takes the height of a horses mom and the height of a horses dad, divides them together, and then adds a random number between -.2 - .2 and displays it on the screen.

 

The problem is, horses height is measured by hands, which means 4 inches per hand. It is writen like: 14.2 (14 hands and 2 inches). There is no such thing as 14.4 or 14.2343 (height goes from 14.3 to 15.0, there are only decimals in the 10th place). There shouldn't be a horse smaller than 12.0 or taller than 18.0

 

Now how in the WORLD would I be able to create this with PHP?

 

P.S. SQL is also involved, the height of mom and dad is stored in a database and then the new height of the new horse is stored in the database as well.

 

Help please?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/181270-solved-incrementing-not-by-1/
Share on other sites

Hmm..you got me thinking.

 

What if I did the addition (momheight+dadheight)/2 + rand(.1, .3)

 

In this case, say the average of mom and dad is 15.1 and the random number generated was .3234, I could round that number to .3, then do multiple if statements where if a the height is between 15.35-16, the number equals 16.

 

I think I figured it out. Thanks!

So I had a quick look at this and thought I would give it a try

 

function newHeight($heightMum, $heightDad)
{
$handsMum = calcHands($heightMum);
$handsDad = calcHands($heightDad);
$handsAvg = ($handsMum + $handsDad) / 2;
$handsRand = mt_rand(-2, 2) / 10 * 4;
return checkHeight(round(($handsAvg + $handsRand) / 4, 1));
}

function calcHands($height)
{
$heightInt = floor($height);
$heightDec = $height - $heightInt;
return ($heightInt * 4) + ($heightDec * 4);
}

function checkHeight($height)
{
$heightDec = $height - floor($height);
$height = ($heightDec > 0.3) ? floor($height) + 1 : $height;
return min(max($height, 12), 18);
}

$heightMum = 13.2;
$heightDad = 15.1;

echo newHeight($heightMum, $heightDad);

 

My logic was to try and find the average between the hands value of each horse and then add the random value.

However, from the small testing I did I never saw .3 come up :confused: probably due to my poor maths

 

I hope this helps in some way :shrug:

 

Edit: This wasn't solved when I went to reply :S

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.