Biased coin flipping?

前端 未结 1 1967
温柔的废话
温柔的废话 2021-01-14 08:25

What is the simplest (does not have to be fastest) way to do a biased random choice between True and False in Python? By \"biased\", I mean where either True or False is mor

1条回答
  •  不要未来只要你来
    2021-01-14 08:50

    It's pretty easy and fast:

    import random
    
    def biased_flip(prob_true=0.5):
        return random.random() < prob_true
    

    Of course if you just call biased_flip() you'll get True and False with 50% probability each, but e.g biased_flip(0.8) will give you about eight Trues for each False in the long run.

    0 讨论(0)
提交回复
热议问题