PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

round> <rad2deg
Last updated: Fri, 18 Jul 2008

view this page in

rand

(PHP 4, PHP 5)

rand — Generate a random integer

Description

int rand ( void )
int rand ( int $min , int $max )

If called without the optional min , max arguments rand() returns a pseudo-random integer between 0 and RAND_MAX. If you want a random number between 5 and 15 (inclusive), for example, use rand (5, 15).

Note: On some platforms (such as Windows) RAND_MAX is only 32768. If you require a range larger than 32768, specifying min and max will allow you to create a range larger than RAND_MAX, or consider using mt_rand() instead.

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.

Parameters

min

The lowest value to return (default: 0)

max

The highest value to return (default: RAND_MAX)

Return Values

A pseudo random value between min (or 0) and max (or RAND_MAX, inclusive).

ChangeLog

Version Description
Since 3.0.7 In versions before 3.0.7 the meaning of max was range . To get the same results in these versions the short example should be rand (5, 11) to get a random number between 5 and 15.

Examples

Example #1 rand() example

<?php
echo rand() . "\n";
echo 
rand() . "\n";

echo 
rand(515);
?>

The above example will output something similar to:

7771
22264
11



round> <rad2deg
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
rand
Prince Mabandla
09-Jul-2008 06:08
Okay, here is the final version of a simple function to generate random (but unique) numbers. As I said earlier, it will use less memory when an explicit range is supplied, (the smaller the better). You could always customize it, e.g. the hash algorithm, the return value once it has exhausted the values within a supplied range, etc.

    function urand($min = NULL, $max = NULL){
        static $alreadyGenerated = array();
        $range = ($min && $max) ? ($max - $min) + 1 : NULL;

        do{
            $randValue = ($range) ? rand($min, $max) : rand();
            $key = md5($randValue);
            if(count($rangeList) == $range) return NULL;
            if($range) $rangeList[$key] = $randValue;
        }while($alreadyGenerated[$key]);

        unset($rangeList);
        $alreadyGenerated[$key] = $randValue;
        return $randValue;
    }
Prince Mabandla
09-Jul-2008 02:45
A follow up on the simple class to generate a random (but unique) number. It obviously uses less memory when you specify a small range.

class UniquelyRandom{

    private static $alreadyGenerated = array();
   
    public function urand($min = NULL, $max = NULL)
    {

        $range = ($min) && ($max) ? ($max - $min) + 1 : NULL;

        do{
            $randValue = ($range) ? rand($min, $max) : rand();
            $key = md5($randValue);
            if(count($rangeList) == $range) return NULL;
            if($range) $rangeList[$key] = NULL;
        }while(self::$alreadyGenerated[$key]);               
        unset($rangeList);
   
        self::$alreadyGenerated[$key] = $randValue;
        return $randValue;
    }
}
Anonymous Coward
29-Jun-2008 08:29
This one is better:

function random_letters ($numofletters=10) {
  $v="";
  for($i=0;$i<$numofletters;$i++) $v.=chr(rand(65,90));
  return $v;
}
gacek at my dot planet
29-May-2008 04:36
For some people who would need to generate random string:

<?php

function random_letters ($numofletters) {
    if (!isset(
$numofletters)) $numofletters = 10; // if $numofletters is not specified sets to 10 letters
   
$literki = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'W');
   
$ilosc_literek = count($literki);
    for (
$licz = 0; $licz < $numofletters; $licz++) {
   
$rand = rand(0, $ilosc_literek-1);
   
$vercode = $vercode.$literki[$rand];
    }
}

?>
rammichael@home
24-May-2008 05:11
I always thought the RAND_MAX on windows is 0x7FFF (=32767)
davidsteinsland [at] gmail [dot] com
26-Apr-2008 12:27
emad_ramahi at hotmail dot com:
I've actually noticed that with a large dataset (100k rows), the query dramatically slows down the server and performance is way too bad.

The way I see it, you have to workable solutions:

Using PHP:

<?php
//$Table holds the name of the table we're getting the random row from
//$Rows specifies how many rows we need to fetch
function mysql_rand ($Query, $Rows = 1) {
   
//getting the table name from $query
    //what you can do, is replace the $query argument with $table,
    //this way to dont have to search for the table's name
   
$SQL = sprintf ('SELECT COUNT(*) FROM %s', substr ($query, stripos ($query, 'from')+5, strpos ($query, ' ')));
   
   
$Max = mysql_result (mysql_query ($SQL), 0);
   
$Random = rand (0, $Max);
   
   
//If the random number is 99, and the database only has 100 rows
    //We'll subtract the random number, so we don't exceed 100, thus preventing a MySQL error message
   
return $Query . sprintf ('LIMIT %d, %d', ($Max < $Rows) ? 0 : (($Random > $Max - $Rows) ? $Max - $Rows : $Random), $Rows);
}

//Instead of using MySQL's RAND(), we use LIMIT to fetch rows
//E.g. LIMIT 5,9 fetches from row 5, and the subsequent 9
$SQL = mysql_rand ('SELECT row FROM table'); //SELECT row FROM table LIMIT x, y
?>

or SQL:
SELECT * FROM Table T JOIN (SELECT FLOOR(MAX(ID)*RAND()) AS ID FROM Table) AS x ON T.ID >= x.ID LIMIT 1;
emad_ramahi at hotmail dot com
09-Mar-2008 01:16
Hi All,
For those whom wants to get a random value from MySQL:
select coumnName from TableName order by rand()
Anonymous
25-Feb-2008 12:37
Using the script below with rand() instead of mt_rand(),
I was suprised to find many "words" and part of "phrases" in common.

mt_rand() is highly recomanded when you want to build test cases.

 $ressbdd = mysql_connect("localhost", "usr", "pass")
  or  die("Could not connect: " . mysql_error());
 for($i=0;$i<100000;$i++){
  $xx1=mt_rand(15,80);
  $texte = phrase($xx1);
  $sql = 'INSERT into phrases.valeurs set valeur = \'' . $texte . '\'';
  mysql_query( $sql , $ressbdd );
 }
//=============================================
function phrase($n){
 $phrase='';
 for($i=0;$i<$n;$i++){
  $xx2=mt_rand(2,20);
  $phrase.=mot($xx2) . ' ';
 }
 return(substr($phrase,0,-1).'.'); // phrase ends with a .
}
//=============================================
function mot($n){
 if($n<=0)  return 'hello';
 if($n>=31) return 'world';
 $voyelle =Array('a','e','i','o','u','y');
 $consonne=Array(
'b','c','d','f','g','h','j','k','l','m',
'n','p','q','r','s','t','v','w','x','z');
 $mot='';
 for($i=0;$i<$n;$i++){
  if($i%2==0){
   $xx3=mt_rand(0,19);
   $mot.=$consonne[$xx3];
  }else{
   $xx4=mt_rand(0,5);
   $mot.=$voyelle[$xx4];
  }
 }
 return $mot;
}
Anonymous
06-Nov-2007 01:24
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.
Ishtar
10-Sep-2007 06: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 12: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
23-May-2007 05: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 03: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
07-Mar-2007 08: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 06: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 05: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 05: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
06-Jun-2005 10: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 02: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 06: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.

round> <rad2deg
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites