Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Percentage chance to make action

Tags:

python

random

Simple problem:

percentage_chance = 0.36  if some_function(percentage_chance):    # action here has 36% chance to execute    pass 

How can I write some_function, or an expression involving percentage_chance, in order to solve this problem?

like image 691
methyl Avatar asked Jul 08 '10 11:07

methyl


2 Answers

You could use random.random:

import random  if random.random() < percentage_chance:     print('aaa') 
like image 135
SilentGhost Avatar answered Oct 02 '22 11:10

SilentGhost


import random if random.randint(0,100) < 36:     do_stuff() 
like image 21
Blue Peppers Avatar answered Oct 02 '22 11:10

Blue Peppers