True or false output based on a probability

后端 未结 4 520
梦毁少年i
梦毁少年i 2020-12-09 07:22

Is there a standard function for Python which outputs True or False probabilistically based on the input of a random number from 0 to 1?

example of what I mean:

4条回答
  •  无人及你
    2020-12-09 07:53

    If you want to amass a lot of data, I would suggest using a map:

        from numpy import random as rn
        p = 0.15
        data = rn.random(100)
        final_data = list(map(lambda x: x < p, data))
    

提交回复
热议问题