I am looking for a high-performance Python solution to the following problem:
Flip a biased coin n times so that the probability of heads (=1) is
equal to a given probability p. n is in the millions.
You are looking for the NumPy built-in
Let's verify -
In : p = 0.8 In : n = 100000 In : (np.random.choice([1,0],n,p=[p,1-p])==1).mean() Out: 0.80003999999999997
Looks pretty efficient too -
In : %timeit np.random.choice([1,0],n,p=[p,1-p]) 100 loops, best of 3: 4 ms per loop