, 1. , , .
[0, 1], random.random(). , . , , . , (0,5, 0,75) , 2, 0,25 .
import random
import sys
def pick_random(prob_list):
r, s = random.random(), 0
for num in prob_list:
s += num[1]
if s >= r:
return num[0]
print >> sys.stderr, "Error: shouldn't get here"
:
import collections
count = collections.defaultdict(int)
for i in xrange(10000):
count[pick_random(prob_list)] += 1
for n in count:
print n, count[n] / 10000.0
:
1 0.498
2 0.25
3 0.0515
4 0.0099
5 0.0899
6 0.1007
: . , , . , (, 0.99999999) , . , 1, .