Real Python random percentage -
i don't understand syntax have found in real python book , hoping clarity.
from __future__ import division random import random total_a_wins = 0 total_b_wins = 0 trials = 100000 trial in range(0, trials): a_win = 0 b_win = 0 if random() < .87: # 1st region a_win += 1 else: b_win += 1 # determine overall election outcome if a_win > b_win: total_a_wins += 1 else: total_b_wins += 1 print "probability wins:", total_a_wins/trials print "probability b wins:", total_b_wins/trials so in exercise state has 87% chance of winning. how random () < .87 define 87%?
when read it states: if random less .87
this hoping clarify because random being less .87 doesn't make sense me.
the return value of random.random() uniformly distributed across range [0.0, 1.0) (so 0.0 inclusive 1.0 exclusive), has equal chance of hitting value in range.
that means 87% of time, values below .87 chosen.
if change test random() < 1.0, test pass always, 100% of time. if change random() < 0.0, it'd never pass, 0% of time. , since distribution uniform, random() < 0.5 true half of time, since other half of time values in range [0.5, 1.0) picked instead.
you @ dice roll; 100% of time, you'll roll value < 7 standard 6-sided dice. 0% of time you'll roll value < 1, 50% of time you'll roll value < 4 (1, 2 or 3), , 66.67% of time you'd roll value less 5 (so 2/3rds of rolls). random.random() return value has much larger range 6 distinct values.
Comments
Post a Comment