Write an algorithm to generate numbers from 1 to 7 with equal probability using a specified function that produces random numbers between 1 and 5.

Practice this problem

Suppose the specified function is random(), which generates random numbers from 1 to 5 with equal probability. The idea is to use the expression 5 × (random() - 1) + random() which uniformly produces random numbers in the range [1–25]. So if we exclude the possibility of the random number being one among [8–25] by repeating the procedure, we are left with numbers between 1 and 7 having equivalent probability.

How this works?

Since random() returns random numbers from 1 to 5 with equal probability, R = 5 × (random() - 1) can be any of 0, 5, 10, 15 or 20. Now for the second random() call, let’s explore all possibilities:

If R = 0, R + random() can be any of 1, 2, 3, 4, 5
If R = 5, R + random() can be any of 6, 7, 8, 9, 10
If R = 10, R + random() can be any of 11, 12, 13, 14, 15
If R = 15, R + random() can be any of 15, 16, 17, 18, 19, 20
If R = 20, R + random() can be any of 21, 22, 23, 24, 25

So, the expression uniformly distributes the numbers in the range [1–25]. Following is the C, Java, and Python implementation of the idea:

C


Download  Run Code

Java


Download  Run Code

Python


Download  Run Code

Output (will vary):
 
1 ~ 14.29%
2 ~ 14.25%
3 ~ 14.33%
4 ~ 14.27%
5 ~ 14.26%
6 ~ 14.31%
7 ~ 14.29%

To minimize the total number of calls to the random() function, stop the while loop at r <= 21 and use the modulo operator, as shown below: