Generate 0 and 1 with Equal Probability

一个有问题的解法:

上述解法在i,j取值为0,0和1,1时,均造成死循环。正确的做法应该是把原始随机函数放入while中。

1
2
3
4
5
6
7
8
9
10
11
12
#小年糕面试题
# given A() return 0 1 probability: p 1-p
# implement B() return 0 1 probability: 50% 50%

def B():
while(True):
i = A()
j = A()
if((0 == i) and (1 == j)): # p*(1-p)
return 0
elif((1 == i) and (0 == j)): # (1-p)*p
return 1