Pseudo-random vs. Truly Random

Original author: Bo Allen
  • Transfer
Below is a translation of Bo Allen's article from here .

A simple visual example

I once came across Random.org , a cool real random number generator service. The difference between a real random number generator (GNSS) and a pseudo random number generator (PRNG) is that the GNSS uses unpredictable physical means to generate numbers (e.g. atmospheric noise), and the PRNG uses mathematical algorithms (fully computer generated). You can learn more about this on Random.org (English) and on Wikipedia (English) .

I then fiddled with the Raster Generator and decided to create a pseudo-randomly generated raster for comparison. And you won’t believe it, the very first thing I tried showed a pattern!

Random.org

Random bitmap based on atmospheric noise

PHP rand () on Windows

Random bitmap based on PHP's rand () function in Windows

Oh! Not so "random", huh?

Few PRNGs will create such an obvious pattern as this. It just got a really bad combination of language (PHP), operating system (Windows) and function ( rand()). I ran the same code in Linux and there wasn’t such an obvious pattern. I also ran this code again on Windows, but now I applied a PHP function mt_rand()that uses the Mersenne Whirlwind to better generate random numbers, and there was no obvious pattern. If you want to know more about why this happens, read this .

Here is the code I used to generate the rasters:

// Requires the GD Library
header("Content-type: image/png");
$im = imagecreatetruecolor(512, 512)
    or die("Cannot Initialize new GD image stream");
$white = imagecolorallocate($im, 255, 255, 255);
for ($y=0; $y<512; $y++) {
    for ($x=0; $x<512; $x++) {
        if (rand(0,1) === 1) {
            imagesetpixel($im, $x, $y, $white);
        }
    }
}		
imagepng($im);
imagedestroy($im);

In fact, such things should not bother you with a real random number generator, unless security is somehow violated (there really is a whole separate topic). Pseudo random number generators vary greatly in quality. Some are terrible, some are outstanding, but none are real.

Also popular now: