6.5 Randomness
Many programming languages have functions that return pseudo-random numbers. The prefix ‘‘pseudo’’ is there because they are not truly random numbers but nevertheless do well on statistical measurements of how well-distributed the results are.
Given four possible events E0, E1, E2, and E3 with associated probabilities p0, p1, p2, and p3, how might we use random numbers to simulate these events happening?
Suppose
random()
function returns a random real number r such that 0.0 ≤ r < 1.0. Based on the value of r we get, we determine that one of the E0, E1, E2, and E3 events occurred.
If you are not using Python, use whatever similar function is available in your programming language and environment.
The general scheme is to run the following steps in order:
- r < p0 then we have observed E0 and we stop. In the example...