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.

Examples:

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.

## Java

`// Java code to generate random numbers ` `import` `java.math.BigInteger; ` `import` `java.util.Random; ` ` ` `public` `class` `RandomImp{ ` ` ` ` ` `public` `int` `random01Generator() { ` ` ` ` ` `Random rand = ` `new` `Random(); ` ` ` `return` `rand.nextInt(` `2` `); ` ` ` `} ` ` ` ` ` `// function will use the above ` ` ` `// method and return numbers ` ` ` `// between 0 and 6 inclusive. ` ` ` `public` `void` `random06Generator(){ ` ` ` ` ` `Random rand = ` `new` `Random(); ` ` ` `int` `val = ` `7` `; ` ` ` `while` `(val >= ` `7` `) { ` ` ` ` ` `String res = ` `""` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `3` `; i++) ` ` ` `res += ` ` ` `String.valueOf(random01Generator()); ` ` ` ` ` `BigInteger bg = ` `new` `BigInteger(res,` `2` `); ` ` ` ` ` `val = bg.intValue(); ` ` ` `} ` ` ` ` ` `System.out.println(val); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args){ ` ` ` `RandomImp r = ` `new` `RandomImp(); ` ` ` `r.random06Generator(); ` ` ` `} ` `} ` |

Output :

3 2 4 1

This question is inspired from this stackoverflow link

## leave a comment

## 0 Comments