Created
September 4, 2009 09:41
-
-
Save javascripter/180816 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
# encoding:utf-8 | |
class WeightedList: | |
def __init__(self, lis): | |
self.list = lis | |
self.total = sum(map(lambda v: v[0], self.list)) | |
def choice(self): | |
import random | |
index = random.randint(0, self.total) | |
acc = 0 | |
for i in self.list: | |
acc += i[0] | |
if acc > index: return i[1] | |
if __name__ == "__main__": | |
# http://www.stat.go.jp/data/kokusei/2005/index.htm | |
prefectures = WeightedList([ | |
(5541, u"東京都"), | |
(4652, u"大阪府"), | |
(3515, u"神奈川県"), | |
(1827, u"埼玉県"), | |
(1367, u"愛知県"), | |
(1149, u"千葉県"), | |
(1009, u"福岡県"), | |
(957, u"北海道"), | |
(661, u"兵庫県"), | |
(581, u"沖縄県"), | |
(574, u"京都府"), | |
(545, u"香川県"), | |
(507, u"茨城県"), | |
(488, u"静岡県"), | |
(401, u"滋賀県"), | |
(391, u"奈良県"), | |
(371, u"長崎県"), | |
(359, u"佐賀県"), | |
(340, u"広島県"), | |
(325, u"宮城県"), | |
(318, u"群馬県"), | |
(313, u"栃木県"), | |
(306, u"三重県"), | |
(282, u"石川県"), | |
(275, u"岡山県"), | |
(264, u"富山県"), | |
(263, u"愛媛県"), | |
(251, u"熊本県"), | |
(250, u"山口県"), | |
(226, u"和歌山県"), | |
(199, u"徳島県"), | |
(199, u"岐阜県"), | |
(199, u"山梨県"), | |
(198, u"福井県"), | |
(197, u"新潟県"), | |
(194, u"鹿児島県"), | |
(193, u"大分県"), | |
(175, u"鳥取県"), | |
(163, u"長野県"), | |
(154, u"青森県"), | |
(154, u"福島県"), | |
(151, u"宮崎県"), | |
(133, u"山形県"), | |
(115, u"高知県"), | |
(114, u"島根県"), | |
(103, u"秋田県"), | |
(93, u"岩手県"), | |
]) | |
while True: | |
print prefectures.choice() | |
import time | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment