I am trying to create a random number generator that generates random numbers from 0-11. But I need to design that, given that I already have a random number generator that generates a random number from 0-5. All numbers from 0-11 should be generated with equal probability.
I looked at this link
The reference uses the used equation 5*foo() + foo() -5 , where foo() generates the number 1-5 (not 0-5)
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.
Now can I change the function that modulates it to 12, and return the function, if the number is greater than 24, in the function that is defined in the link above? If not, then I do not understand what is wrong.
Alternatively, I came across this
allows you to call the random number generator function f(6) , which generates the number 0-5.
(f(6)+f(6)+f(6))%12;
If not, what alternative solution can I subtract? I need help with this task. Maybe I missed something. The catch here, each number between 0-11 should have an equal probability of generation. Other than f(6) I cannot use any other function. Only mathematical manipulations.
source share