Problem: Given a function that produces a random integer between 1 and some value m, write a function that produces a random integer between 1 and some other value n.
Solution: Generate the sum of n random numbers in range (1, m) and then find the remainder mod n; add 1 to make the range of results begin at 1.
-
This directory contains a program
simulate_random_range.pycontaining the functionmain(given, desired)
where `given` is _m_ in the description above and `desired` is _n_. There is also a second function,
comprehension(given, desired)
that performs the same computation but does so as a list comprehension rather than a for-loop; this function turns out to be slightly slower than `main()`.
-
The directory also contains a program
test_randomness_simulate_random_range.py, whose functionmain(trials=100, given=5, desired=7)
returns the ratio of standard deviation to mean for our simulated function and the actual results of random.randint(1, desired), as well as the proportion of these two ratios, which we predict never to be further apart than 1:3.
- There is also a
py.testtest in atest/directory, which asserts that the 1:3 bound holds on the proportion.
[end]