Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/55468-solved-help-generating-number/
Share on other sites

$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...

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);
?>

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);

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.