Session 5: Probability - Module C: Random Number Generator

1. Overview

Learn about how random numbers are generated on a computer.
Write a pseudo-random number generator.

2. Setting Up

Download randomNumbers.py:

  1. Download the Python program randomNumbers.py into your programs directory.

Start IPython:

  1. Open a terminal window.
  2. Change the directory to your programs directory with the cd command.
  3. Start IPython with the ipython command.
  4. From IPython, you can run your program by typing run randomNumbers.py.

3. How do Computers Generate Random Numbers?

In the previous modules, you have been working closely with functions that produce random numbers and results, but were those numbers truly random? Take a moment to think about what it means for something to be random. Note your thoughts down on your webpage.

Now consider the following:

  1. You call a function, passing a single parameter "hello". The function returns the sequence 46, 85, 66, 60, 84, 29, 38. Do these numbers seem random?

  2. You call the function again, passing a single parameter "world". The function returns the sequence 52, 94, 90, 61, 75, 65, 96. Do these numbers seem random?

  3. You call the function a third time, passing the parameter "hello" again. The function returns the sequence 46, 85, 66, 60, 84, 29, 38. Do these numbers seem random?

This is called pseudo-random number generation. It uses a mathematical formula to produce numbers that appear to be random. If you provide the same input, you will get the same sequence of numbers every time. In order to produce truly random numbers, computers need an additional source of randomness. This is usually gathered from the environment around them.

4. Writing a Pseudo-Random Number Generator

Creating a pseudo-random number generator that produces numbers with equal probability and that cannot be predicted is a difficult task. You will be competing against your peers to produce the best pseudo-random number generator with these two properties. Open randomNumbers.py in gedit to set started. Keep trying to improve the formula.
Tip: When you find a good formula, leave it in the code by commenting it out with # instead of deleting it.

Take a screenshot of your code and answer these questions on your webpage!