Percentage chance to make action

前端 未结 3 1703
长情又很酷
长情又很酷 2020-12-13 14:24

Simple problem: percentage_chance = 0.36

if :
   #action here has 36% chance to execute
   pass

How can i solve thi

相关标签:
3条回答
  • 2020-12-13 15:05

    You could use random.random:

    >>> import random
    >>> if random.random() < percentage_chance:
        print('aaa')
    
    0 讨论(0)
  • 2020-12-13 15:13
    import random
    if random.randint(0,100) < 36:
        do_stuff()
    
    0 讨论(0)
  • 2020-12-13 15:28

    This code returns a 1, 36% of the time

    import random
    import math
    chance = 0.36
    math.floor( random.uniform(0, 1/(1-chance)) )
    
    0 讨论(0)
提交回复
热议问题