Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.