Random Numbers
Posted by: John in Programming on Aug 05, 2008
Have you ever been in a conversation where someone blurts out something completely off topic? You say to yourself, “well, that was random.” Chances are, unless that person suffers from a mental disability, that spew of “random” verbiage was not really random.
What is randomness?
The Oxford English Dictionary defines random as “made, done, or happening without method or conscious decision.” More than likely, your conversation sparked some conscious relocation which resulted in the person sharing their memory at an obscure time. Randomness brings rise to philosophical questions: Are a persons actions random or destiny? Physical questions: Is the motion of an electron around an atoms nucleus random?
As a programmer, one of the questions I always asked myself is “How random are these 'rand()' functions?” That is the question I will attempt to answer.
There are two approaches for generating “random” numbers using a computer.
-Pseudo-Random Number Generators (PRNG)
-True-Random Number Generators (TRNG)
True-random Number Generators
These type of number generators are not shipped as native language construct. This is because they rely on some physical phenomena such as radio active decay, atmospheric noise, or the photoelectric effect, which, software cannot mirror. As the name suggests, the random numbers generated by this method are truly random – or at least as random as we can get. It is this method that is used by casinos, the lottery, advanced cryptography. Because I am no genus, you should refer you to wikipedia for more information regarding TRNG
Pseudo-Random Number Generators
These are the types of number generators that are seen vastly in computation, and thus programming. A PRNG is nothing more than a predefined algorithm that in most cases seems random. To understand why it seems random lets paint a picture: Each time I roll a die, it has a random result. If I roll the die X amount of times, and record each result, I will end up with a list of randomly generated numbers. You can think of a PRNG as simply traversing this list. Although a PRNG is not actually a list, it is a mathematical formula which can produce varying results. One of the easiest, oldest, and best know formula is the linear congruential generator. Infact this formula is used by Borland C/C++ and glibc which is used by the GCC complier. Complex PRNG's with particular seeds (the initial values for the formula) can produce results that seem just as random as TRNG's.
I wanted to see how random php's rand() PRNG actually was. To test the function, used the GD library to plot 100,000 random x and y locations points on a canvass. The code I used is below:

Using the results of an online TRNG, I produced the following visualization:
Using the above code on my LAMP server produced seeming random results that closely paralleled that of the TRNG:
However, using the same code on my WAMP server, a pattern is clearly visible:
Therefor it is reccomended you use the mt_rand() function which is based on the mersenne twister algorithm which is faster and produces better results.
What is randomness?
The Oxford English Dictionary defines random as “made, done, or happening without method or conscious decision.” More than likely, your conversation sparked some conscious relocation which resulted in the person sharing their memory at an obscure time. Randomness brings rise to philosophical questions: Are a persons actions random or destiny? Physical questions: Is the motion of an electron around an atoms nucleus random?
As a programmer, one of the questions I always asked myself is “How random are these 'rand()' functions?” That is the question I will attempt to answer.
There are two approaches for generating “random” numbers using a computer.
-Pseudo-Random Number Generators (PRNG)
-True-Random Number Generators (TRNG)
True-random Number Generators
These type of number generators are not shipped as native language construct. This is because they rely on some physical phenomena such as radio active decay, atmospheric noise, or the photoelectric effect, which, software cannot mirror. As the name suggests, the random numbers generated by this method are truly random – or at least as random as we can get. It is this method that is used by casinos, the lottery, advanced cryptography. Because I am no genus, you should refer you to wikipedia for more information regarding TRNG
Pseudo-Random Number Generators
These are the types of number generators that are seen vastly in computation, and thus programming. A PRNG is nothing more than a predefined algorithm that in most cases seems random. To understand why it seems random lets paint a picture: Each time I roll a die, it has a random result. If I roll the die X amount of times, and record each result, I will end up with a list of randomly generated numbers. You can think of a PRNG as simply traversing this list. Although a PRNG is not actually a list, it is a mathematical formula which can produce varying results. One of the easiest, oldest, and best know formula is the linear congruential generator. Infact this formula is used by Borland C/C++ and glibc which is used by the GCC complier. Complex PRNG's with particular seeds (the initial values for the formula) can produce results that seem just as random as TRNG's.
I wanted to see how random php's rand() PRNG actually was. To test the function, used the GD library to plot 100,000 random x and y locations points on a canvass. The code I used is below:

- header("Content-type: image/png");
- $im = @imagecreatetruecolor(500, 500)
- or die("Cannot Initialize new GD image stream");
- $white = imagecolorallocate($im, 255, 255, 255);
- for ($i = 0; $i < 100000; $i++) {
- imagesetpixel($im, rand(0,500), rand(0,500), $white);
- }
- imagepng($im);
- imagedestroy($im);
- ?>
header("Content-type: image/png");
$im = @imagecreatetruecolor(500, 500)
or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($im, rand(0,500), rand(0,500), $white);
}
imagepng($im);
imagedestroy($im);
?>
$im = @imagecreatetruecolor(500, 500)
or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for ($i = 0; $i < 100000; $i++) {
imagesetpixel($im, rand(0,500), rand(0,500), $white);
}
imagepng($im);
imagedestroy($im);
?>
Using the results of an online TRNG, I produced the following visualization:
Using the above code on my LAMP server produced seeming random results that closely paralleled that of the TRNG:
However, using the same code on my WAMP server, a pattern is clearly visible:
Therefor it is reccomended you use the mt_rand() function which is based on the mersenne twister algorithm which is faster and produces better results.
