True or false output based on a probability

匆匆过客 提交于 2019-12-03 05:22:11
import random

def decision(probability):
    return random.random() < probability
Blindy

Given a function rand that returns a number between 0 and 1, you can define decision like this:

bool decision(float probability)
{
   return rand()<probability;
}

Assuming that rand() returns a value in the range [0.0, 1.0) (so can output a 0.0, will never output a 1.0).

Paul

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))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!