Generate two random numbers first number smaller than second php - php

I need to generate two Random numbers using PHP. This is simple but the condition is that the first number should be smaller than second number in value.
So let's say I need to generate lower and upper bounds between 10 and 1000. So I will need two random numbers within this range. First number as lower bound (20 may be) and upper bound higher than that in value so (100 may be).
Thanks
Ahmar.

$num1 = rand(20, 999);
$num2 = rand($num1+1, 1000);

$smaller = mt_rand(10, 999); // This generates a number between 10 and 999.
$bigger = mt_rand($smaller+1, 1000); // generates a bigger number up to 1000 based on the smaller number
Is there anything wrong with just generating two numbers between 10 and a thousand and then ordering them? Or does the size of the second one have to be dependent on the first? Because if not,
$rand = mt_rand(10, 999);
$rand2 = mt_rand(10, 999);
while($rand === $rand2) $rand2 = mt_rand(10, 999);
$bigger = max($rand, $rand2);
$smaller = min($rand, $rand2);

Related

How can I generate 30 random numbers from 1 to 100 in PHP

I want to generate 30 random numbers from 1 to 100, and avoid a number from getting generated twice. So for example, out of the 30 random numbers, the number 9 won't be generated more than once.
How can I do that?
An efficient approach could be:
You can generate a range of numbers from 1 to 100.
Random Shuffle the array.
Select first 30 values out of randomised array, using array_slice function.
It shall be random and distinct values. Try:
// Generate an array of numbers from 1 to 100
$numbers = range(1,100);
// Random shuffle the array
shuffle($numbers);
// Take first 30 values out of the array (it will be random and distinct)
$random_30 = array_slice($numbers, 0, 30);
Keep adding numbers until there are 30, each time use in_array and mt_rand to get an unused random number between 1 and 100.
$random_numbers = [];
while(count($random_numbers) < 30){
do {
$random_number = mt_rand(1,100);
} while (in_array($random_number, $random_numbers));
$random_numbers[] = $random_number;
}
var_dump($random_numbers);

rand(000000, 999999) sometimes generate 4 digit numbers

I want to generate 6 digit numbers.
Now this work great BUT occasionally it generates 4 digit numbers. Not often but some times it does. Why??
$num = rand(000000, 999999);
$num = rand(100000, 999999);
Maybe this do the job :)
If you want to generate numbers from 000000 to 999999 with 6-digit padding, you can use the str_pad function.
$rand = rand(0, 999999);
echo str_pad($rand, 6, '0', STR_PAD_LEFT);
rand(000000, 999999) is equal to rand(0, 999999)
It will return a number between 0 and 999999. In 90% of all cases the number is between 100000 and 999999 and you will have a 6 digit number. That is why it works for you most of the time
But in 10% of all cases, the number will be smaller than 100000 and only contain 1 to 5 digits (all numbers between 1 and 99999..not hard to figure out that 1 or 2 digits are still less propable then 4 or 5 digits)
To solve your problem you have to get a number from rand(100000, 999999), but this won't contain any numbers starting with 0! The first digit will always be from 1 and 9.
The other answers already show nice solutions for getting 6 digits from 0 to 9. Another easy one would just be:
for($i = 0; i < 6; i++)
$rand_digit[$i] = rand(0,9);
As everyone else said, you could change $num = rand(000000, 999999); to $num = rand(100000, 999999);, but there might be a case where you need a number that has 6 digits, but whose value is below 100000. Ex. 001103. You can still use $num = rand(000000, 999999); but you would use something like:
$num = rand(000000, 999999);
$print_num = sprintf("%06d", $num);
This would not change the number, it will only give it a 6 digit format.
http://php.net/manual/en/function.rand.php
Note :
Warning
min max range must be within the range getrandmax(). i.e. (max - min) <= getrandmax() Otherwise, rand() may return poor-quality random numbers.
So, an other note :
Note: On some platforms (such as Windows), getrandmax() is only 32767. If you require a range larger than 32767, specifying min and max will allow you to create a range larger than this, or consider using mt_rand() instead.

Checking a number's factor

I'm not sure if this title is correct but here's basically what I am trying to do.
I am trying to check if a number is less than 100 and if it isn't I would like to know what factor of 10 I need to divide it by to get below 100 i.e. for 7923 the factor is 100 to make it 79.23 and for 452,936,489 the factor would be 10,000,000 to make it 45.2936489.
Is there a function or a piece of script that does that out there?
Cheers
$number = 452936489;
$factor = pow(10, ceil(log($number/100) / log(10)));
Ok. basic math:
you need to find a power of 10 divisor that reduces your number below 100, so the log business figures out the exact fractional power of 10 required to turn 10 into your original number. That comes out to be around 6.6560373....
That gets rounded up to 7, and is then used to raise 10 to that power.
10^7 = 10,000,000
452936489 / 10^7 = 45.2936489
<?
$num = 7923;
$x = 10;
while(true)
{
$result = $num/$x;
if($result < 100)
{
die($x."");
}
else
{
$x *= 10;
}
}
?>

How to choose 1 random number from 2 different number ranges?

I need to get a random number that is between
0 - 80
and
120 - 200
I can do
$n1 = rand(0, 80);
$n2 = rand(120, 200);
But then I need to choose between n1 and n2. Cannot do
$n3 = rand($n1, $n2)
as this may give me a number between 80 - 120 which I need to avoid.
How to solve this?
Since both ranges have different sizes (even if only by 1 number), to ensure good random spread, you need to do this:
$random = rand( 0, 200 - 39 );
if ($random>=120-39) $random+=39;
Fastest method. :)
The way this works is by pretending it's a single range, and if it ends up picking a number above the first range, we increase it to fit within the second range. This ensures perfect spread.
Since both ranges have the same size you can simply use rand(0, 1) to determine which range to use.
$n = rand(0, 1) ? rand(0, 80) : rand(120, 200);
PHP has a new function for this as well called range. Very easy to use, and can be located in the PHP Manual.
It allows you to input a minimum/maximum number to grab a range from.
<?php
echo range(0, 1000);
?
Technically though, you could also enter your own two numbers to serve as the number range.
get two random numbers $n1 and $n2
$n1 = rand(0, 80);
$n2 = rand(120, 200);
define new array called $n3
$n3=array();
add $n1 and $n2 into array $n3 use array_push() function
array_push($n3,$n1,$n2);
use array_rand() function to find random index $find_index from array $n3.
$find_index=array_rand($n3,1);
show the result
echo $n3[$find_index];

How to generate a random positive or negative decimal?

How can I regenerate random decimal from -0.0010 to 0.0010 with php rand() or some other method?
Divide rand() by the maximum random numer, multiply it by the range and add the starting number:
<?php
// rand()/getrandmax() gives a float number between 0 and 1
// if you multiply it by 0.002 you'll get a number between 0 and 0.002
// add the starting number -0.001 and you'll get a number between -0.001 and 0.001
echo rand()/getrandmax()*0.002-0.001;
?>
.
$val = (rand(0,20)-10)/10000;
This uses two rand() calls but I think that the readability makes up for it tenfold.
The first part makes either a -1 or +1. The second part can be anything between 0 and your limit for +/- numbers.
$rand = (rand(0,1)*2-1)*rand(0, 100);
echo $rand;
Unless you require LOTs of random numbers in a gigantic loop, you probably won't even notice the speed difference. I ran some tests (50.000 iterations) and it came up to around 0.0004 milliseconds to get a random number by my function. The alternatives are around half that time, but again, unless you are inside a really big loop, you are probably better of optimizing somewhere else.
Speed testing code:
$start = microtime();
$loopCount = 50000;
for($i=0;$i<$loopCount;$i++)
{
(0*2-1)*rand(0, 100);
}
$end = microtime();
echo "Timing: ", ((($end-$start)*1000.0)/((float)$loopCount)), " milliseconds.";
This will return any possible number between -0.001 and +0.001
$random = ((rand()*(0.002/getrandmax()))-0.001)
// or without paranthesis:
$random = rand()*0.002/getrandmax()-0.001
$randselect=rand(0,(array_sum($adarray)*100000000));
$cumilativevalue=0;
foreach ($adarray as $key => $value) {
$cumilativevalue=$cumilativevalue+$value*100000000;
if($randselect<$cumilativevalue){$selectedad=$key;break;}
}
Random float with one decimal between -1,1
$random = round((rand(0,1) - floatVal('0.'.rand(0,9).rand(0,9))), 1);

Categories