COIS 4470H – Lab 5: Investigating Random Number Generators
The random module in Python implements several pseudo random number generators
(PRNGs) with support for various distributions.
There are many desired properties of pseudo random number generators including:
1- Uniformity
• The generated numbers should be evenly distributed across the possible range.
• Each number should have an equal probability of appearing.
2 - Independence
• Each generated number should be independent of previous numbers.
• There should be no detectable patterns or correlations between successive
numbers.
3- Reproducibility
• Given the same seed, a PRNG should generate the same sequence of
numbers.
• This is useful for debugging simulations.
4 - Efficiency
• The RNG should generate numbers quickly with minimal computational
overhead.
5- Range Control
• The RNG should allow users to specify bounds for the generated numbers.
The random module in Python implements a pseudo random number generator based on the
Mersenne Twister a general purpose PRNG developed in 1997 by Makoto Matsumoto and Takuji
Nishimura. It is not suitable for generating random numbers that will be used in cryptographic
algorithms because it is not cryptographically secure.
Cryptographically secure PRNGs require different properties than general purpose PRNGs such as
the Mersenne Twister including but not limited to:
1- Forward secrecy: Knowing past values should not help predict future values.
2- Backward secrecy: Knowing future values should not reveal past values.
Cryptographically secure PRNGs aren’t a good fit for generating random numbers for simulations
because they are not reproducible. If you use them in your simulation, and you would like to have
the same result to debug an error in your simulation you will run into issues.
In this lab we will test the uniformity and independence of numbers generated using the random
module.
You may use any Python IDE you wish. If you do not have Python installed you can use Google
Colab but you will need to log in with a Google account (your Trent U email will not work). In
order to install libraries create a new code chunk and add a ‘!’ at the start of pip. It should look
like !pip install scipy
Step 1: Create a Python script
Step 2: Import the random module
Step 3: Install the scipy module (pip install scipy) and import it into your script.
Step 4: Install the statsmodels module (pip install statsmodels) and import it into your script.
Step 5: Generate 10,000 random numbers and put them in a list.
Step 6: Statistically test the uniformity of numbers generated by the PRNG using the Chi-
Squared Goodness of Fit Test.
Step 7: Comment on the results of the Chi-Squared Goodness of Fit Test that you obtained.
• Focus on the p value. What does a p value indicate? What does a high p value indicate?
Step 8: Test the independence of the numbers by the PRNG using the Wald-Wolfowitz Runs Test.
Step 9: Comment on the results of the Wald-Wolfowitz Runs Test.
Step 10: Demonstrate your completed work to your lab demonstrator(s) before you leave.