A Guide to Random Numbers

Repl Talk Badge

Introduction

Have you ever wondered how random numbers work? In this tutorial, I'll be explaining how the Python random module works. This tutorial/explanation is for Python, but it will be similar or the same for other languages.

Are random numbers random?

Short answer: No.

Long answer: There is no such thing as true randomness on a computer. An example of true randomness is the rolling of a dice. Random numbers on a computer are not random. They are pseudo-random numbers.

What are pseudo-random numbers?

"Pseudorandomness measures the extent to which a sequence of numbers, though produced by a completely deterministic and repeatable process, appear to be patternless." - Wikipedia

This means that although random numbers look completely random, they're not, and they can be repeated.

Examples

I've created a python file with the following code:

      
import random

for i in range(10):
    print(random.randint(1, 10), end=" ")
      
  

This will print 10 "random" numbers.

Running the program, here is my output: 3 2 1 4 5 4 3 9 2 6

If I run it again, I get different numbers: 1 6 2 7 2 6 5 3 7 4

Seems random right? Well what if I told you that we could make it produce the same numbers every time?

Add this line of code before the for loop:

      
random.seed(1)
      
  

Running the program I get this output: 3 2 5 2 8 8 8 7 4 2

If I run it again, I get the same output: 3 2 5 2 8 8 8 7 4 2

This is because, by default, random.seed() uses the seconds past the Epoch. The Epoch is the 1st January 1970.

Let's import time and set the seed to the current time.

      
import time

time = time.time()
print(time)
random.seed(time)
...
      
  

Now running the program here is my output:

    
1632677787.1545157
7 6 6 3 1 7 3 5 7 8
    

The first line is the current time. This is in seconds past the Epoch. Then we have our pseudo-random numbers. If we set our random.seed() to the first number, we'll always get the same random numbers generated.

There is a very popular game called Minecraft that uses pseudo-random numbers. In the generation of a world, you can enter a seed. This seed then generates the world. But, if you use the same seed multiple times, the world is always the same. This is the same principle. The worlds are generated based on the seed, just like our numbers are here!

Finished code:

      
import random
import time

time = time.time()
print(time)
random.seed(time)

for i in range(10):
    print(random.randrange(1, 10), end=" ")
      
  

To learn more about Python's random module, check out the documentation: https://docs.python.org/3/library/random.html

Modified version of code:

      
import random
import time


with open('numbers.txt', 'a') as b:

    b.write('\nNew Session\n')
    print('\nNew Session\n')
    
    for a in range(100):
        current_time = time.time()
        print(f'Time: {current_time}')
        b.write(f'Time: {current_time}\n')
        random.seed(current_time)

        print('Random Numbers: ', end="")
        for c in range(10):
            print(random.randrange(1, 10), end=" ")
            b.write(str(random.randrange(1, 10)))
            b.write(' ')
        print('\n')
        b.write('\n')
      
  

This will run the generator 100 times and write the results into a file called numbers.txt.

Repl: @DillonB07/pseudo-randomNumbersDemo

Repl Talk Badge
Back