pocobueno1388 Posted June 13, 2007 Share Posted June 13, 2007 I have a script where I am trying to generate a random number that is between two numbers...but it gets more complicated. Lets say the numbers are: 14.1 and 16 The numbers between these would be like this: 14.1, 14.2, 14.3, 15, 15.1, 15.2, 15.3, 16 Notice how the number never gets past 3 tenths, it skips up to the next whole number, then the pattern continues. So if the numbers are 14.1 and 16, I would like it to randomly choose between the numbers listed above. The numbers are always different, so I need a script that will be able to do this no matter what the two starting numbers are. Here is the script I attempted: <?php function height($breed){ //Smaller number $height[0] = "14.2"; //Larger Number $height[1] = "16"; //Divide front and back numbers, front number being the number before decimal, back being the one after the decimal $num1 = explode('.', $height[0]); $num2 = explode('.', $height[1]); //Get random front number $front = rand($num1[0], $num2[0]); //get random back number if (empty($num1[1])) $num1[1] = 0; if (empty($num2[1])) $num2[1] = 0; $back = rand(0, $num2[1]); if (empty($back)) $back = rand(1,3); if ($back == 0) $back = ""; $fheight = $front.'.'.$back; //If the final height ends up being bigger than it should be, redo the calculations if ($fheight > $height[1]) height($breed); else echo $fheight; } ?> This code was very close to working okay, but it had flaws in it. Like if the bigger number was 16, it would never end up as "16". If anyone can suggest a good way to do this, it would be greatly appreciated =D Thanks xD Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/ Share on other sites More sharing options...
DJTim666 Posted June 13, 2007 Share Posted June 13, 2007 The only way I would think you would be able to tdo this is making the number count stop at (number).3 then restart at the next highest number. Try google? Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274120 Share on other sites More sharing options...
Full-Demon Posted June 13, 2007 Share Posted June 13, 2007 $random_number = (int) rand(14, 16).'.'.rand(0, 3); Not sure it that works...but I should do it like that. OR rand(14, 16) + (rand(0, 3)/10) FD Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274122 Share on other sites More sharing options...
pocobueno1388 Posted June 13, 2007 Author Share Posted June 13, 2007 $random_number = (int) rand(14, 16).'.'.rand(0, 3); Not sure it that works...but I should do it like that. OR rand(14, 16) + (rand(0, 3)/10) FD Those methods don't take in account that if the highest number was 16, then it couldn't be like 16.3, because that wouldn't be between the two numbers... Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274126 Share on other sites More sharing options...
DJTim666 Posted June 13, 2007 Share Posted June 13, 2007 That first bit of code will ouput unwanted results; 16.1, 16.2, and 16.3. Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274128 Share on other sites More sharing options...
sasa Posted June 13, 2007 Share Posted June 13, 2007 try <?php function my_rand($a, $b, $c=3) { $a = floor($a) * (++$c) + ($a - floor($a)) * 10; $b = floor($b) * $c + ($b - floor($b)) * 10; $x = rand($a, $b); $out = floor($x / $c) * 10 + ($x % $c); return $out / 10; } $start = 14.1; $end = 16.2; echo my_rand($start, $end); ?> Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274142 Share on other sites More sharing options...
Link Posted June 13, 2007 Share Posted June 13, 2007 I made a very quick solution that is really roundabout but still accomplishes the goal: function rand_num($low, $high) { if ($low == 0 || $high == 0) return; $numbers = array(); $num = $low; $count = 0; while ($num <= $high) { $num = round($num, 1); $dec = (($num*10) % 10); if ($dec > 3) { $num += ((10 - $dec) * .1); $numbers[$count] = $num; $num += .1; } else { $numbers[$count] = $num; $num += .1; } $count++; } $array_len = count($numbers) - 1; $random = rand(0, $array_len); return $numbers[$random]; } print rand_num(10.3, 13); Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274146 Share on other sites More sharing options...
pocobueno1388 Posted June 13, 2007 Author Share Posted June 13, 2007 Thank you all very much =D That solves the problem. Quote Link to comment https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/#findComment-274159 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.