FYI.

This story is over 5 years old.

Tech

How To Make a Random Number

Or at least a number that can fake it well enough.
​ENIAC. Image: ​Seth Tisue/Flickr

​Making random numbers is a hot business in computer science. Graphics and cryptography, in particular, rely on randomness or at least randomness that's good enough to fool users/hackers/gamers. Mostly, randomness is fake—the ​most common random number generator, implemented in programming languages from C++ to Python to PHP, really delivers only pseudo-randomness (too pseudo for cryptography) at the expense of an intensive CPU workout.

Advertisement

That's fine, mostly. The natural chaos that computer graphics schemes often seek to emulate is itself fake randomness; randomness is after all just a function of available information and not some property of the universe. (Quantum mechanics muddies this a bit, yes.) Chaos is all the more intriguing in its apparent impossibility, and yet people and algorithms remain easily fooled.

It so happens that producing random numbers is an old task. At a conference in 1949, the mathematician John von Neumann presented an algorithm for random number generation called the method of middle-squares. Intended for use with ENIAC, the world's first true computer, middle-squares can be done relatively simply by hand. That would make sense given the method's proper origin (​it's thought) in a medieval monastery circa 1245, courtesy of a Franciscan friar.

Von Neumann came up with the middle-squares scheme independently, however, and he usually gets the digital-age credit. ​It works like this: 1) Grab a seed number, which is just some number pulled from the air; random-number functions in computers often use dates and times for this purpose. So, for a four-digit seed (where length/number of digits n = 4), we can just use 1213. 2) Square the seed. 3) We need an 8 digit number (where the length is 2n), so if the result of our square is less than 8 digits, we'll need to add 0s to the front as padding.

Advertisement

Finally (step 4), we grab the middle four digits. So, for our 8 digit number, we peel away the first 2 and last 2 digits. Now, we take those remaining 4 digits and repeat.

Like this, we can generate a sequence of decent pseudorandom numbers. The method was pretty handy back when computers were slow, but the method implodes in a couple of ways. For one, it has a short period, which in terms of randomness, means that the generated sequences will repeat fairly quickly as you continue the steps above. Second, sequences produced via middle-squares all eventually spiral downward toward 0. In mathspeak, we say they converge to 0.

Convergence is a fact of life in randomness. Numbers produced pseudo-randomly over some range of values eventually settle down toward a constant number, which is where the random sequence dies. Middle squares then will show diminishing returns as we poke it for more and more sequences, though we can prolong the magic by starting with larger seeds.

Von Neumann was aware of this limitation, of middle-squares and randomness generally, and ​in a 1951 paper offered his famous quote (one of them): "Any one who considers arithmetical methods of producing random digits is, of course, in a state of sin. For, as has been pointed out several times, there is no such thing as a random number—there are only methods to produce random numbers."