How do I simulate flip of biased coin in python?

2020-02-08 11:06发布

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:

def flip(p):
   '''this function return H with probability p'''
   # do something
   return result

>> [flip(0.8) for i in xrange(10)]
[H,H,T,H,H,H,T,H,H,H]

7条回答
叛逆
2楼-- · 2020-02-08 11:49
  • Import a random number between 0 - 1 (you can use randrange function)

  • If the number is above (1-p), return tails.

  • Else, return heads

查看更多
登录 后发表回答