def dealHand(n):
"""
Returns a random hand containing n lowercase letters.
At least n/3 the letters in the hand should be VOWELS.
Hands are represented as dictionaries. The keys are
letters and the values are the number of times the
particular letter is repeated in that hand.
n: int >= 0
returns: dictionary (string -> int)
"""
hand={}
numVowels = n/3
for i in range(numVowels):
x = VOWELS[random.randrange(0, len(VOWELS))]
hand[x] = hand.get(x, 0) + 1
for i in range(numVowels, n):
x = CONSONANTS[random.randrange(0,len(CONSONANTS))]
hand[x] = hand.get(x, 0) + 1
return hand
這個函數是一個文字遊戲,我不得不做的一部分,它被列入了一些輔助功能,幫助上手,我的問題是,它的回報是不是很隨機的字母,有很多重複的字母,如:a a c c b e e g j j m m m o o r t v y x
,我只是想知道是否有可能得到一個更隨機的字符集?的文字遊戲蟒蛇隨機字符
你有沒有打電話給random.seed()?必須調用種子才能初始化隨機數生成器。 – 2013-03-20 17:13:29
不,您不必調用'random.seed',當模塊第一次導入時,使用當前系統時間來初始化發生器。 – Matthias 2013-03-20 17:18:04
你能解釋一下你想散文(而不是代碼)嗎? – 2013-03-20 18:00:26