Random Numbers Aren’t So Random

If computers only do what we tell them, how do they create random numbers?

Photo by Jonathan Petersson on Pexels.com

Random numbers are all around us, particuarly when we look at computers. Our “auto-generated” passwords, the amount of coins you win for logging in daily to your favorite game, and, of course, the =RAND() Excel function – all random. So where do these random numbers come from? Is there some magical random place within your computer?

Like all things in computer (quantum computers excluded), things just don’t happen on their own. Computers do what they’re programmed to do. The same applies to random numbers. Not to burst your bubble, but those “random” numbers aren’t actually random, as we’ll see. In fact they’re made with simple algorithms you can quickly create yourself.

Origins of Random Numbers

To create random numbers, we typically use a Random Number Generator (RNG) (but of course we do…). The first RNG was devised by John von Neumann in the 1940’s. Many current methods still piggyback off of his initial work. Von Neumann suspected that he could start with any number he could think of (known as a seed), square it, and slice out the middle numbers. So if we started with 675248, we’d then square it to get 455959861504, we’d then slice out the middle numbers (959861) to arrive at our “random number” . From there, we could then use 959861 as our seed to repeat the process, getting 51 as our second “random” number.

Set Seed675248
Scrape out Middle as our Random Number959861
Set new Seed959861

As you can see, there’s really nothing random about this method. It’s computed using a simple equation, yet it does produce values that appear random to us. Because of these two properties, we’ll call these number pseudo-random numbers (PRN). Today’s algorithms commonly utilize the same foundation, but of course have advanced significantly. Most continue to start with an initial number (seed), perform a computation, and reiterate that computation with the last result. Von Neumann’s work isn’t used today because people noticed the “random numbers” quickly start to repeat themselves in this cycle. Today’s algorithms are commonly optimized to repeat only after billions or trillions of runs.

Create Your Own Random Number Generator

A simple, but pretty useful random number generator is called the Linear Congruent Generator (LCG) – and yes, it sounds much worse than it really is. Like Von Neumann, you start with a seed. We then multiply the seed by a special prime number, and the perform a modulo with it and another prime number. (These prime numbers are selected to ensure they cycle repeats only after very long runs). We then plug our random number back into the system as the new seed.

#Simple Linear Congruential Generator (LCG)

import numpy as np

def generator(seed, n,):
    #create an empty list to store our n random numbers
  array_of_rn = np.empty(n)

  for i,_ in enumerate(array_of_rn):
    random_number = np.mod((58598 * seed), 233280)
      #save the random number
    array_of_rn[i] = random_number
      #reset the seed to the last random number
    seed = random_number

  return array_of_rn


## array([ 23948., 125704., 186992., 195616.,  27008.,  43264., 130112.,
        12736.,  41408.,  80704.])

What Happens with Bad Random Number Generators?

Remember those “special prime numbers” we talked about in the last section? Yes, those really are needed. Let’s see why. Below is a plot of 100 randomly generated numbers using our algorithm above (I generated random x’s and random y’s and plotted).

As you can see, everything really does look random. Now, let’s use the exact same algorithm, same seeds, same everything, except change those special prime numbers to 2 and 8. Again, we’ll generate 100 points using two lists of random numbers.

Depict Bad RNG

No, it’s not a mistake. You only see 3 points. Why? Because without those special primes, our algorithm continually repeats itself. In this case, each cycle is 3 points and the same 3 “random numbers” appear over and over again.


Hopefully you’ve learned a little about how those random-numbers you see are made. If you look close, you’ll start to see them everywhere – especially with all of those new two-factor authentication apps. Of course today’s top-of-the-line RNGs are much more complex than the simple ones we’ve covered – and will likely get even more complex with the rise of quantum computing. But for now, the underlying mechanics are the same. They’re not truly random, but they’re the best we can do for now and they generally do the trick.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: