[英]How do you create a probabilistic conditional flow in python?
我正在考慮一個條件流,該條件流大約一半時間或一半時間整數。 我的代碼是這樣的:
import random
for i in range(10):
binarychoice=random.randint(0,1)
if binarychoice=0:
pass #do whatever you need to do, return an integer
elif binarychoice=1:
pass #do whatever you need, return a float
這個對嗎? 但是,有沒有可用的庫來執行這樣的概率條件流(因為我從未真正聽說過,主要是由於缺乏經驗),因此我可以簡單地沿“ X ...或Y”的方向寫一些東西而執行X或Y的概率是一半?
如果只有50/50,這會更干凈一些:
import random
for i in range(10):
if random.randint(0,1):
pass #do whatever you need to do, return an integer
else:
pass #do whatever you need, return a float
或通過以下可能性獲得更大的靈活性:
import random
for i in range(10):
if random.random() < .8:
pass #do whatever you need to do, return an integer
else:
pass #do whatever you need, return a float
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.