Sometimes you want to take random actions in your code - it might be to give your web site visitors a different greeting each time they visit, you might be programming a game, or you might be trying to secure data by hashing it. Either way, randomisation is simple and helpful thing to remember, and has just two functions: rand(), and mt_rand().

Both functions do the same thing, and both take the same parameters, so what is the difference between the two? Well, rand() is a basic randomisation function that is very quick but not very "random" - the numbers it generates are slightly more predictable. Mt_rand() on the other hand, is more complicated - the "mt" parts means Mersenne Twister, as that is the name of the randomisation algorithm it uses. Mt_rand() returns much more "random" numbers, but does so at the expense of some speed.

As mentioned, both functions have the same parameters - two optional numbers, for the minimum number to return and the maximum number to return. Either you supply no parameters, which will result in PHP returning a random number between one and a very high number, or you can supply the two parameters. Here is an example:

Note that the two numbers passed in are inclusive. That is, our $randrange number could be anywhere between 1 and 10 including 1 and 10.

As mentioned, if you do not pass any parameters to your rand() and mt_rand() calls, PHP will generate a random number from 1 to a high number. If you want to find out the maximum number PHP can return from a rand() call, use getrandmax(). There is a similar function, mt_getrandmax() for mt_rand().

Now you know how randomisation works, here is a quick example to show you how you can make use of randomisation to greet web site visitors in various ways:

Here we have not bothered assigning the result of rand() to a variable before putting it into the switch statement, but you can do it whichever way is easier for you to read.

One important thing to note is that the speed of randomisation does not depend on the sizes you pass into it - rand() is just as fast in rand(1,3) as it is in rand(1, 10000000). Mt_rand() works just short of 50% slower than rand(), which means you should only be using it if you particularly need the extra randomisation it brings.

To give you an idea of how fast the two run and how using larger values for randomisation makes no difference, try this script:

<?php
$START=time();
for ($i=1;$i<1000000; ++$i) { $j=rand(1,100);
} $END=time() -$START;
print"Short rand() took $END seconds\n";

$START=time();
for ($i=1;$i<1000000; ++$i) { $j=mt_rand(1,100);
} $END=time() -$START;
print"Short mt_rand() took $END seconds\n";

$START=time();
for ($i=1;$i<1000000; ++$i) { $j=rand(1,10000000);
} $END=time() -$START;
print"Long rand() took $END seconds\n";

$START=time();
for ($i=1;$i<1000000; ++$i) { $j=mt_rand(1,10000000);
} $END=time() -$START;
print"Long mt_rand() took $END seconds\n"; ?>

Most random number generators require "seeding" - initialising with a starting value - because the numbers they generate are not truly random. Instead, they are known as pseudo-random, meaning that they appear to be random. The seed value is used to generate the first number, the first number is used to generate the second number, the second for the third, etc, meaning that if you always supply the same seed value you will always get the same string of "random" numbers.

This is actually advantageous. Many years ago there was a popular game called Elite available on the BBC Micro, where the player was allowed to fly around a large universe of eight galaxies, each with thousands of star systems. Each star system had a very precise number of planets, a distinct economy situation, etc, and yet the entire universe fitted into just 22K of memory. How was this possible? Simple: by providing the same seed to their random number generator, the exact same universe could be generated each
time.

If you want your random number to always generate the same string of numbers, simply supply a seed that is a known value. For example, not matter how often you run it, this next script will always generate the same "random" numbers: