簡體   English   中英

如何在python中生成具有特定概率的隨機數?

[英]How to generate random numbers with specific probabilities in python?

例如,

嘗試生成只有0 and 1的 1000 個隨機數的列表。

0 has the prob of 40%

1 has the prob of 60%

謝謝!

我的概率 = []

for i in range(1000):
#creates one number out of 0 or 1 with prob p 0.4 for 0 and 0.6 for 1
    test = numpy.random.choice(numpy.arange(0, 2), p=[0.4, 0.6])
    myProb.append(test)

打印(我的問題)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM