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