#!/usr/bin/python
# -*- coding: utf-8 -*-
import random
l = [
('平凡之路', 89),
('夜空中最亮的星', 95),
]
s = reduce(lambda s, x: s + x[1], l, 0)
def rand_song():
choose = random.randint(1, s)
for song, weight in l:
choose -= weight
if choose <= 0:
return song
if __name__ == '__main__':
# test
import time
random.seed(time.time())
d = {}
for i in xrange(100000):
song = rand_song()
if song in d:
d[song] += 1
else:
d[song] = 1
for k, w in l:
v = d[k]
print k, v, 1.0*v/100000, 1.0*w/s