Designing a random number generator

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.

+6
source share
2 answers

There are many ways to do this, but in this case I would go for:

 f(6) + 6*f(2) 

Where

 f(2) = f(6)%2 

The probability is uniform, because you have a uniform probability of getting a number from 0 to 5 and a uniform probability of shifting it by 6-11 intervals.

+2
source

Let your generator name 0..5 g6 (). If you calculate (6 * g6 ()) + g6 (), this will give you the form 0..35. Now just divide by 3 and you will have a uniform of 0..11.

+1
source

Source: https://habr.com/ru/post/985484/


All Articles