Skip to content
Advertisement

How do I simulate flip of biased coin?

In unbiased coin flip H or T occurs 50% of times.

But I want to simulate coin which gives H with probability ‘p’ and T with probability ‘(1-p)’.

something like this:

JavaScript

Advertisement

Answer

random.random() returns a uniformly distributed pseudo-random floating point number in the range [0, 1). This number is less than a given number p in the range [0,1) with probability p. Thus:

JavaScript

Some experiments:

JavaScript
Advertisement