Question: Part Five - Analysis How do you know that a RNG is working well enough? There are quite a few tests that mathematicians run to

Part Five - Analysis
How do you know that a RNG is working well enough? There are quite a few tests that mathematicians run to ensure they have a good setup for RNGs. We will only run two very simple tests, as we are simply learning about how these algorithms work.
In your rng.py file, add a class called Analyzer.
Instance Variables:
max: maximum value returned by the RNG (integer)
min: minimum value returned by the RNG (integer)
average: average valure returned by the RNG (float)
period: period of the RNG (integer)
bit_freqs: list of integers containing the frequency of bits in the numbers returned by the RNG. This list should be ordered from least significant bit to most significant bit.
Methods:
__init__(self, rand_num_gen): constructor, where rand_num_gen is an instance of one of your RNGs. This means you will need to create the RNG before creating an instance of the Analyzer.
analyze(self, max_nums=1e10): method that iterates over the RNG given to it until the RNG stops iteration or it has generated max_nums numbers.
The list bit_frequencies is the trickiest. We need to figure out the largest number our RNG will return, then count the number of bits required to store that number. For instance, if the largest number our RNG will return is 32, then we would need
100000
six bits to store the largest value. So we would create
bit_freqs=[0 for _ in range(0,6)]
to hold counters for each of the bits. Then, for each number the RNG returns we would get the binary representation (read the python binary documentation. For instance, if the RNG returned
, the bit representation would be
Bit: 543210
----------------
001101
So we would add one to the counter in bit_freqs[0], bit_freqs[2], and bit_freqs[3]. At the end of the analysis sequence (when the RNG stops iteration or has produced max_nums numbers), the bit_freqs list will tell us how often each bit was used in the overall sequence. We call this the Monobit Test. A truly random sequence should have a uniform distribution - meaning each bit is just as likely as the next to be used. In practice though, this is rarely the case. Here is an example from a (very poor) RNG test I performed (this example turned the frequencies into percentages by dividing each by the total number of numbers seen).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!