Given a function random01Generator() that gives you randomly either 0 or 1, implement a function that utilizes this function and generate numbers between 0 and 6(both inclusive). All numbers should have same probabilities of occurrence.
on multiple runs, it gives 3 2 3 6 0
Approach : The idea here is to find the range of the smallest number greater than the given range. Here, since range is 6, decide that 2^3 is the smallest number greater than 6. Hence, k here will be 3.
So, try forming a binary number of 3 characters each time and once all 3 characters are obtained, look up for the corresponding decimal representation of that binary number and return that value.
For e.g., if it gives 0 in first random01 call, then 1 in next call and finally again 1 in next call, it can be said that binary number thus formed is 011 which is decimal representation of number 3 and hence, return 3. If in case it gives 111, which is 7 which out of range. So, just discard this number and repeat the whole process again until it gives the number in the required range.
On calling random01Generator() 6 times, the probability of occurrence of numbers won’t be same. For e.g., for first time, the probability of occurrence of 0 is 1/2. Now, for second time, again the probability is 1/2 which makes total probability as 1/4. If this process is repeated 6 times by calling the random function 6 times, this will make the probability of occurrence of 0 as 1/(2^6). Similarly, the probability of occurrence of 1 will be greater than that of 0, of 2 will be greater than 1 and so on, following the binomial distribution pattern.
Doing above way will ensure that all probabilities are equal as it is considering only that range in which one can generate those numbers and rejecting any cases giving us result greater than the maximum specified range. Hence, the above approach.
Obviously, this idea can be extended, if there is some other random generator, say random0mGenerator() and there is need to generate number between 0 to n, where n>m. In this case, modify below function to incorporate m and n.
Prerequisites : BigInteger in Java.
3 2 4 1
This question is inspired from this stackoverflow link
- Implement rand3() using rand2()
- Implement rand12() using rand6() in one line
- Maximum String Partition
- Maximum length sub-array which satisfies the given conditions
- Generating Random String Using PHP
- Generating OTP (One time Password) in PHP
- Shuffle or Randomize a list in Java
- Mid-Square hashing
- Random Walk (Implementation in Python)
- Expectation or expected value of an array
- Estimating the value of Pi using Monte Carlo
- Binomial Random Variables
- Randomized Binary Search Algorithm
- Shuffle a deck of cards
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.