I am trying to get around this problem of my own making. I want to generate 0 or 1 with another function(gr0_4()) which generates random number from 0 to 4.
I am wondering if I can approach this way:
a). if gr0_4() = 0 or 1 then I will use 0
b). if gr0_4() = 2 or 3 then I will use 1
c). if gr0_4() = 4 I will repeat the a) and b) steps.
Is my understanding correct that the "a" and "b" step each has 50% probability of happening?
def gr0_1():
while True:
x = gr0_4()
if x == 0 or x == 1:
return 0
elif x == 0 or x == 1:
return 1
What if I want to use gr0_1() to create gr1_7() i.e. create number between 1 to 7 with equal probability?
Can I use below reasoning to create that function gr1_1().
As 7 consists of 3 bits. I can generate each bit with equal probability using gr0_1(). So I will call gr0_1() three times and based on that value I get, I will set/unset the corresponding bits to generate a number between 1 to 7 including the numbers 1 and 7. However I can get the number 0 but I don't want that so I will repeat the process again. Will the probability of each number generation between 1 to 7 will be 1/7 in that case also?
Some simple mathematical calculation will be nice to answer this. I tried to read up on rejection sampling but couldn't understand much.