Python:随机分配两个值中的一个

时间:2013-07-03 23:45:32

标签: python

if losttwice <= 2:
    bet = _________  # <- Here
elif losttwice <= 5:
    bet = bet * 2
else:
    bet = startingbet

任何人都可以帮我添加一件事吗?我希望losttwice <= 2(当我丢失1-2次时)有机会随机50%,因为它有bet = startingbetbet = bet * 2基于50%的几率。

5 个答案:

答案 0 :(得分:2)

if random.random() > 0.5:(以及顶部的import random)可能很有用。你应该能够根据它来搞清楚。

答案 1 :(得分:2)

if losttwice <= 2:
    bet = random.choice((startingbet, bet*2))

答案 2 :(得分:1)

Python有一个很好的模块用于生成伪随机性。可以找到文档here。如果您决定使用random.choice,代码将如下所示:

from random import choice
if losttwice <= 2:
    bet = choice((startingbet, 2 * bet))
elif losttwice <= 5:
    bet = bet * 2
else:
    bet = startingbet

答案 3 :(得分:0)

导入随机

if losttwice <= 2:
        if random.random() > 0.5:
            bet = startingbet
        else:
            bet = bet * 2
elif losttwice <= 5:
    bet = bet * 2
else:
    bet = startingbet

答案 4 :(得分:0)

if losttwice <= 2:      
    bet = random.choice([staringbet, bet*2])
elif losttwice <= 5:
    bet = bet * 2
else:
  bet = startingbet