Note, the function used by PHP to constrain a random number between (min, max) is the following:
$number = $min + (($max - $min + 1) * ($number / ($rand_max + 1));
where the following:
$number - the initially generated random number
$min - the minimum in the range
$max - the maximum in the range
$rand_max - the maximum possible random value
What this algorithm does is constrain the generated number to a 0-1 range, then multiply it against your range, mapping the two to each other.
In practice you'll see the following results:
For a generated $number of 16384 and a $rand_max of 32768:
rand(0, 10) = 5
rand(0, 100) = 50
rand(0, 1000) = 500
with the additional property that if the range you're asking for is larger than $rand_max, random numbers will be in a multiple of $max/$rand_max.
rand
(PHP 4, PHP 5)
rand — 乱数を生成する
説明
int rand
([ int $min
], int $max
)
オプションの引数 min ,max を省略してコールした場合、rand() は 0 と RAND_MAX の間の擬似乱数(整数)を返します。 例えば、5 から 15 まで(両端を含む)の乱数を得たい場合、 rand(5,15) とします。
注意: (Windows のような)いくつかのプラットフォームでは、RAND_MAX は 32768 と小さな値となっています。 32768 より広い範囲にしたい場合、 min および max を指定することで、 RAND_MAX より大きな範囲の乱数を生成することができます。 もしくは、 mt_rand() をかわりに使用してみてください。
注意: PHP 4.2.0 以降、 srand() または mt_srand() によりランダム数生成器にシードを与える必要はありません。 これは、この処理が自動的に行われるためです。
パラメータ
- min
-
返す値の最小値 (デフォルトは 0)。
- max
-
返す値の最大値 (デフォルトは RAND_MAX)。
返り値
min (あるいは 0) から max (あるいは RAND_MAX、それぞれ端点を含む) までの間の疑似乱数値を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 3.0.7 以降 | 3.0.7 より前のバージョンでは、max の意味は range でした。これらのバージョンにおいて 同じ結果を得るために簡単な例を示すと、 5 から 15 までの乱数を得たい場合には rand (5, 11) とする必要があります。 |
例
Example#1 rand() の例
<?php
echo rand() . "\n";
echo rand() . "\n";
echo rand(5, 15);
?>
上の例の出力は、たとえば 以下のようになります。
7771 22264 11
rand
Anonymous
06-Nov-2007 06:24
06-Nov-2007 06:24
Ishtar
10-Sep-2007 10:18
10-Sep-2007 10:18
A small comment on phpdev-dunnbypauls conclusion that rand() only generates numbers that are a multiply of 3.
<?php
$n = rand(0,100000); // with MAX_RAND=32768
?>
Since, 100000/32768=3.05 you get multiples of 3. The random integer will be multiplied by 3.05 to fit between 0 and 100000. rand() works fine, if you don't ask for bigger numbers then RAND_MAX.
rok dot kralj at gmail dot com
16-Jun-2007 04:43
16-Jun-2007 04:43
rand function returns just a whole numbers. If you want a random float, then here's an elegant way:
<?php
function random_float ($min,$max) {
return ($min+lcg_value()*(abs($max-$min)));
}
?>
bozo_z_clown at yahoo dot com
24-May-2007 09:36
24-May-2007 09:36
Note that the automatic seeding seems to be done with the current number of seconds which means you can get the same results for several runs on a fast server. Either call srand() yourself with a more frequently changing seed or use mt_rand() which doesn't appear to suffer from the problem.
jont at live dot co dot uk
05-Apr-2007 07:42
05-Apr-2007 07:42
isn't this just a simpler way of making a random id for somthing? I mean i know that there is a very slight chance that a duplicate could be made but its a very, very, very small chance, nearly impossible.
$rand = mt_rand(0, 32);
$code = md5($rand . time());
echo "$code";
and if you don't want it the md5 can be removed, I've just added it as a prefer it there :)
Jon
phpdev at dunnbypaul dot net
08-Mar-2007 01:51
08-Mar-2007 01:51
Here's an interesting note about the inferiority of the rand() function. Try, for example, the following code...
<?php
$r = array(0,0,0,0,0,0,0,0,0,0,0);
for ($i=0;$i<1000000;$i++) {
$n = rand(0,100000);
if ($n<=10) {
$r[$n]++;
}
}
print_r($r);
?>
which produces something similar to the following output (on my windows box, where RAND_MAX is 32768):
Array
(
[0] => 31
[1] => 0
[2] => 0
[3] => 31
[4] => 0
[5] => 0
[6] => 30
[7] => 0
[8] => 0
[9] => 31
[10] => 0
)
Within this range only multiples of 3 are being selected. Also note that values that are filled are always 30 or 31 (no other values! really!)
Now replace rand() with mt_rand() and see the difference...
Array
(
[0] => 8
[1] => 8
[2] => 14
[3] => 16
[4] => 9
[5] => 11
[6] => 8
[7] => 9
[8] => 7
[9] => 7
[10] => 9
)
Much more randomly distributed!
Conclusion: mt_rand() is not just faster, it is a far superior algorithm.
Patrick Daryll G.
02-Mar-2007 11:51
02-Mar-2007 11:51
Using rand()%x is faster than rand(0,x) yes, but it is wrong.
Consider the following example:
RAND_MAX is 32768 (like on Windows for example)
You use rand()%30000
Imagine rand() returns a value between 30000 and 32768.
Modulo could make any value between 0 and 2768, but not any between 2769 and 29999 (except the value is below 29999).
This would double the chance of getting a number between 0 and 2768, which is speaking against the principles of randomness.
ludicruz at yahoo dot com
27-Sep-2006 09:42
27-Sep-2006 09:42
frank, nick at nerdynick dot com, and kniht
this is now O(n) instead of O(n^2) ish...
<?php
function rand_permute($size, $min, $max)
{
$retval = array();
//initialize an array of integers from $min to $max
for($i = $min;$i <= $max;$i++)
{
$retval[$i] = $i;
}
//start with the the first index ($min).
//randomly swap this number with any other number in the array.
//this way we guarantee all numbers are permuted in the array,
//and we assure no number is used more than once (technically reiterating prev line).
//therefore we don't have to do the random checking each time we put something into the array.
for($i=$min; $i < $size; $i++)
{
$tmp = $retval[$i];
$retval[$i] = $retval[$tmpkey = rand($min, $max)];
$retval[$tmpkey] = $tmp;
}
return array_slice($retval, 0, $size);
}
?>
umpalump at poczta dot neostrada dot pl
13-Jun-2005 09:32
13-Jun-2005 09:32
Random numbers with Gauss distribution (normal distribution).
A correct alghoritm. Without aproximations, like Smaaps'
It is specially usefull for simulations in physics.
Check yourself, and have a fun.
<?php
function gauss()
{ // N(0,1)
// returns random number with normal distribution:
// mean=0
// std dev=1
// auxilary vars
$x=random_0_1();
$y=random_0_1();
// two independent variables with normal distribution N(0,1)
$u=sqrt(-2*log($x))*cos(2*pi()*$y);
$v=sqrt(-2*log($x))*sin(2*pi()*$y);
// i will return only one, couse only one needed
return $u;
}
function gauss_ms($m=0.0,$s=1.0)
{ // N(m,s)
// returns random number with normal distribution:
// mean=m
// std dev=s
return gauss()*$s+$m;
}
function random_0_1()
{ // auxiliary function
// returns random number with flat distribution from 0 to 1
return (float)rand()/(float)getrandmax();
}
?>
JanS
student of astronomy
on Warsaw University
smaaps at kaldamar dot de
07-Jun-2005 02:44
07-Jun-2005 02:44
Lately I needed some random numbers with a gaussian (normal) distribution, not evenly distributed as the numbers generated by rand(). After googling a while, I found out that there is no perfect algrorithm that creates such numbers out of evenly distruted random numbers but a few methods that have similar effect. The following function implements all three algorithms I found- The the last two methods create numbers where you can find a lower and upper boundary and the first one will create a number from time to time (such as one in every 10000) that may be very far from the average value. Have fun testing and using it.
function gauss($algorithm = "polar") {
$randmax = 9999;
switch($algorithm) {
//polar-methode by marsaglia
case "polar":
$v = 2;
while ($v > 1) {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$v = (2 * $u1 - 1) * (2 * $u1 - 1) + (2 * $u2 - 1) * (2 * $u2 - 1);
}
return (2* $u1 - 1) * (( -2 * log($v) / $v) ^ 0.5);
// box-muller-method
case "boxmuller":
do {
$u1 = rand(0, $randmax) / $randmax;
$u2 = rand(0, $randmax) / $randmax;
$x = sqrt(-2 * log($u1)) * cos(2 * pi() * $u2);
} while (strval($x) == "1.#INF" or strval($x) == "-1.#INF");
// the check has to be done cause sometimes (1:10000)
// values such as "1.#INF" occur and i dont know why
return $x;
// twelve random numbers
case "zwoelfer":
$sum = 0;
for ($i = 0; $i < 12; $i++) {
$sum += rand(0, $randmax) / $randmax;
}
return $sum;
}
}
relsqui at armory dot com
21-Jan-2005 07:23
21-Jan-2005 07:23
Don't forget, it's faster to use bitwise operations when you need a random number that's less than some power of two. For example,
<?php
rand()&1;
// instead of
rand(0,1);
// for generating 0 or 1,
rand()&3;
// instead of
rand(0,3);
// for generating 0, 1, 2, or 3,
rand()&7;
// instead of
rand(0,7)
// for generating 0, 1, 2, 3, 4, 5, 6, or 7,
?>
and so on. All you're doing there is generating a default random number (so PHP doesn't have to parse any arguments) and chopping off the piece that's useful to you (using a bitwise operation which is faster than even basic math).
onno at onnovanbraam dot com
12-Sep-2004 10:58
12-Sep-2004 10:58
>>Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.<<
Do realize that you don't HAVE to call srand, but if you don't you get the same results every time... Took me quite a while to get to that conclusion, since I couldn't figure it out:
foreach($Unit AS $index => $value) {
srand() ;
$rand_pos_x = rand(0, $EVAC['x_max']) ;
$rand_pos_y = rand(0, $EVAC['y_max']) ;
}
works, but:
foreach($Unit AS $index => $value) {
$rand_pos_x = rand(0, $EVAC['x_max']) ;
$rand_pos_y = rand(0, $EVAC['y_max']) ;
}
doesnt even though they say it should.
I'm using Windows XP Pro (Windows NT EVOLUTION 5.1 build 2600), PHP Version 4.3.3, CGI/FastCGI.