rand(000000, 999999) sometimes generate 4 digit numbers - php

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.

Related

PHP more elegant way to make random 1 or 0 binary pattern

I am wondering if there is a more elegant way to create a random mix of 4 1's and 0's in PHP. The way I do it works but I am curious if there is a way to do the same thing with less code?
$b1 = rand(0,1);
$b2 = rand(0,1);
$b3 = rand(0,1);
$b4 = rand(0,1);
$randpattern = $b1.$b2.$b3.$b4;
Slightly shorter still:
$randpattern = substr(decbin(rand(16, 31)), -4);
The rand(16,31) will generate a random number between 16 and 31 which is made into a binary number, with decbin(), between 10000 en 11111. Finally the substr() picks only the last four characters.
Sure:
str_pad(decbin(rand(0, 15)), 4, '0', STR_PAD_LEFT);
Call rand() only once. It will give a random number between 0 and 15. 15 in binary is 1111. You can also write 15 in binary to make it clear. rand(0, 0b1111)
Convert into binary.
If number is less than 1000 then left pad it with 0.
You can simply use a loop :
$randpattern = '' ;
while( strlen($randpattern) < 4 )
$randpattern .= rand(0,1);

Controlling likelyhood of randomly generated numbers

If I wanted a random number between one and three I could do $n = mt_rand(1,3).
There is a 33% chance that $n = 1, a 33% chance it's 2, and a 33% chance that it's 3.
What if I want to make it more difficult to get a 3 than a 1?
Say I want a 50% chance that a 1 is drawn, a 30% chance that a 2 is drawn and a 20% chance that a 3 is drawn?
I need a scalable solution as the possible range will vary between 1-3 and 1-100, but in general I'd like the lower numbers to be drawn more often than the higher ones.
How can I accomplish this?
There is a simple explanation of how you can use standard uniform random variable to produce random variable with a distribution similar to the one you want:
https://math.stackexchange.com/a/241543
This is maths.
In your example the just chose a random number between 0 and 99.
Values returned between 0 to 49 - call it 1
Values returned between 50 - 69 - Call it 2
Values returned between 70 - 99 - Call it 3
Simple if statement will do this or populate an array for the distribution required
Assuming a 1 - 10 scale, you can use a simple if statement and have the numbers represent percentages. And just have each if statement set $n to a specific. Only downfall, it isn't universal.
$dummy = mt_rand(1,10);
// represents 50%
if ($dummy <= 5) {
$n = 1;
}
// represents 40%
if ($dummy >= 6 && $dummy <= 9) {
$n = 2;
} else {
// represents 10%
$n = 3;
}

Generate two random numbers first number smaller than second 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);

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 can I separate a number and get the first two digits in PHP?

How can I separate a number and get the first two digits in PHP?
For example: 1345 -> I want this output=> 13 or 1542 I want 15.
one possibility would be to use substr:
echo substr($mynumber, 0, 2);
EDIT:
please not that, like hakre said, this will break for negative numbers or small numbers with decimal places. his solution is the better one, as he's doing some checks to avoid this.
First of all you need to normalize your number, because not all numbers in PHP consist of digits only. You might be looking for an integer number:
$number = (int) $number;
Problems you can run in here is the range of integer numbers in PHP or rounding issues, see Integers Docs, INF comes to mind as well.
As the number now is an integer, you can use it in string context and extract the first two characters which will be the first two digits if the number is not negative. If the number is negative, the sign needs to be preserved:
$twoDigits = substr($number, 0, $number < 0 ? 3 : 2);
See the Demo.
Shouldn't be too hard? A simple substring should do the trick (you can treat numbers as strings in a loosely typed language like PHP).
See the PHP manual page for the substr() function.
Something like this:
$output = substr($input, 0, 2); //get first two characters (digits)
You can get the string value of your number then get the part you want using
substr.
this should do what you want
$length = 2;
$newstr = substr($string, $lenght);
With strong type-hinting in new version of PHP (> PHP 7.3) you can't use substr on a function if you have integer or float. Yes, you can cast as string but it's not a good solution.
You can divide by some ten factor and recast to int.
$number = 1345;
$mynumber = (int)($number/100);
echo $mynumber;
Display: 13
If you don't want to use substr you can divide your number by 10 until it has 2 digits:
<?php
function foo($i) {
$i = abs((int)$i);
while ($i > 99)
$i = $i / 10;
return $i;
}
will give you first two digits

Categories