

Pseudo-Random vs. True Random (2012)
source link: https://boallen.com/random-numbers.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

A Simple Visual Example
Once upon a time I stumbled across Random.org , an awesome true random number generation service. The difference between true random number generators (TRNGs) and pseudo-random number generators (PRNGs) is that TRNGs use an unpredictable physical means to generate numbers (like atmospheric noise ), and PRNGs use mathematical algorithms (completely computer-generated). You can read more about this at Random.org and Wikipedia .
I was messing around with the Bitmap Generator , when I decided to create a pseudo-random generated bitmap of my own to compare. And wouldn't you know it, the very first thing I tried showed a pattern!
Random.org Bitmap

PHP rand() on Windows Bitmap

Yikes! Not so "random" eh?
Not many PRNGs will produce an obvious visual pattern like this, it just so happens to be a really bad combination of language (PHP), operating system (Windows), and function (rand()). I ran the same code in a Linux environment, and it did not have an obvious visual pattern. I also ran the same code again in Windows, but using PHP's mt_rand() function instead, which utilizes the Mersenne Twister to generate a better random number, and it did not have an obvious visual pattern. If you want to know more about why this happens, read this .
Here's the code I used to generate the bitmap:
<?php // 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)) { imagesetpixel($im, $x, $y, $white); } } } imagepng($im); imagedestroy($im);
Basically, it's the sort of thing you wouldn't need worry about with a true random number generator, unless somehow it's security was breached (a whole other topic really). Pseudo-random number generators vary greatly in quality. Some are horrible, some are extraordinary, but none are true .
Recommend
-
52
Contributor
-
59
Yesterday I wrote about the terrible quality of the default pseudo random number generators in the C++ standard library. The article came to the conclusion they're all around terrible, and should generally be avoided, ye...
-
43
The Floated Labels Technique When we’re dealing with inputs, there’s a host of techniques to consider in order to give users the best experience. We need to make sure that we’re giving users necessary informati...
-
43
:zap:️ MJN :zap:️ Simple pseudo-monad to check if a key or a value exists in an object. No...
-
51
OwnerGuy SteeleTypeFeatureScopeSEStatusCandidateComponentcore-libs / java.utilDiscussion
-
14
SHISHUA: The Fastest Pseudo-Random Generator In the World (TLDR: see theand the code .) Six months ago, I wanted to make the best PRNG with an uncon...
-
12
What's this fuss about true randomness? Perhaps you have wondered how predictable machines like computers can generate randomness. In reality, most random numbers used in computer programs are pseudo-random...
-
5
Generating a Random True or False Boolean Value by kirupa | filed under JS Tips and Tricks
-
7
Vectorizing xxHash for Fun and Profit 10 Replies Update: Thanks to Yann Col...
-
9
The Problem I was working on a system where I had to initialize each microcontroller-based device on a communication bus with unique addresses. Since the addressed are supposed to be unique while the devices were identical, the tric...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK