carleihar Posted November 12, 2009 Share Posted November 12, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/181270-solved-incrementing-not-by-1/ Share on other sites More sharing options...
xangelo Posted November 12, 2009 Share Posted November 12, 2009 Are there certain rules to handling the addition of the random number? IE if it is over .3 it will always round up to the next nearest whole number? Quote Link to comment https://forums.phpfreaks.com/topic/181270-solved-incrementing-not-by-1/#findComment-956301 Share on other sites More sharing options...
carleihar Posted November 12, 2009 Author Share Posted November 12, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/181270-solved-incrementing-not-by-1/#findComment-956311 Share on other sites More sharing options...
chocopi Posted November 12, 2009 Share Posted November 12, 2009 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 probably due to my poor maths I hope this helps in some way Edit: This wasn't solved when I went to reply :S Quote Link to comment https://forums.phpfreaks.com/topic/181270-solved-incrementing-not-by-1/#findComment-956316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.