Created
June 27, 2011 07:13
-
-
Save wozozo/1048434 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf8 | |
import random | |
def choice(d): | |
s = 0 | |
for k, v in d.items(): | |
s += v | |
_range = random.randrange(s) | |
if _range < v: | |
n = k | |
return n | |
if __name__ == '__main__': | |
d = {0: 1, 1: 100, 2: 200, 3: 300, 4: 400, 5: 500, 6: 600, 7: 700, 8: 800, 9: 900} | |
test = {} | |
for i in range(1000000): | |
_ = choice(d) | |
if test.has_key(_): | |
test[_] += 1 | |
else: | |
test[_] = 1 | |
for k, v in sorted(test.items(), key=lambda x:x[1], reverse=True): | |
print '%s: %s' % (k, v) | |
# result | |
# 9: 200051 | |
# 8: 177945 | |
# 7: 155636 | |
# 6: 133656 | |
# 5: 111085 | |
# 4: 88258 | |
# 3: 66570 | |
# 2: 44515 | |
# 1: 22059 | |
# 0: 225 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment