Given a function foo() that returns integers from 1 to 5 with equal probability, write a function that returns integers from 1 to 7 with equal probability using foo() only. Minimize the number of calls to foo() method. Also, use of any other library function is not allowed and no floating point arithmetic allowed.
We know foo() returns integers from 1 to 5. How we can ensure that integers from 1 to 7 occur with equal probability?
If we somehow generate integers from 1 to a-multiple-of-7 (like 7, 14, 21, …) with equal probability, we can use modulo division by 7 followed by adding 1 to get the numbers from 1 to 7 with equal probability.
We can generate from 1 to 21 with equal probability using the following expression.
5*foo() + foo() -5
Let us see how above expression can be used.
1. For each value of first foo(), there can be 5 possible combinations for values of second foo(). So, there are total 25 combinations possible.
2. The range of values returned by the above equation is 1 to 25, each integer occurring exactly once.
3. If the value of the equation comes out to be less than 22, return modulo division by 7 followed by adding 1. Else, again call the method recursively. The probability of returning each integer thus becomes 1/7.
The below program shows that the expression returns each integer from 1 to 25 exactly once.
1 2 . . 24 25
The below program depicts how we can use foo() to return 1 to 7 with equal probability.
This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Implement rand12() using rand6() in one line
- Implement rand3() using rand2()
- Generate 0 and 1 with 25% and 75% probability
- Random number generator in arbitrary probability distribution fashion
- Select a random number from stream, with O(1) space
- Reservoir Sampling
- Shuffle a given array using Fisher–Yates shuffle Algorithm
- Print all permutations in sorted (lexicographic) order
- Check divisibility by 7
- Make a fair coin from a biased coin
- Given a number, find the next smallest palindrome
- DFA based division
- Sieve of Eratosthenes
- Magic Square
- Write a program to print all permutations of a given string
Improved By : jit_t