📜 ⬆️ ⬇️

Pseudorandom vs. Truly Random

Below is a translation of Bo Allen's article from here .

A simple illustrative example

Once I stumbled upon Random.org , a cool service for generating real random numbers. The difference between the Real Random Number Generator (GNSS) and the Pseudo-Random Number Generator (PRNG) is that GNSS uses unpredictable physical means to generate numbers (for example, atmospheric noise), and PRNU uses mathematical algorithms (fully produced by computer). You can learn more about this on Random.org (Eng.) And Wikipedia (Eng.) .

I then fiddled with the Raster Generator and decided to create a pseudo-randomly generated raster for comparison. And you will not believe, 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", eh?

Few PRNGs will create such an obvious pattern as this. It's just that a really bad combination of language (PHP), operating system (Windows) and functions ( rand() ) rand() . I ran the same code in Linux and there was no such obvious pattern. I also ran this code again in Windows, but now I used the PHP function mt_rand() , which uses the Mersenne Vortex to better generate a random number, and there was no obvious pattern. If you want to know more about why this is happening, 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 generator of real random numbers, unless security is somehow not compromised (really a whole separate topic). Pseudo-random number generators vary greatly in quality. Some are terrible, some are outstanding, but none are real.

Source: https://habr.com/ru/post/137864/


All Articles