Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Designing a random number generator

I am trying to design a random number generator which generates random numbers from 0-11.But I need to design that considering I already have a random number generator which generates random number from 0-5.All the numbers from 0-11 should be generated with equal probability.

I went through this link

In the link the equation used is 5*foo() + foo() -5 wherein foo() generates 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 which modulus it by 12 and recurse the function if the number surpasses 24, in the function which is defined in the link above?If not then I am not understanding what is wrong.

Alternatively I came upon this one

lets call the random number generator function f(6) which generates number 0-5.

(f(6)+f(6)+f(6))%12;

If not what alternative solution can i deduct?I need help in doing this task.Maybe I am missing something.The catch here is each number between 0-11 should have equal probability of generation.Other than f(6) I cannot use any other function.Only mathematical manipulations.

like image 779
Nilesh Avatar asked Apr 20 '15 05:04

Nilesh


1 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 to obtain a number from 0 to 5, and a uniform probability to shift it to the 6-11 interval.

like image 185
DarioP Avatar answered Oct 16 '22 18:10

DarioP