【发布时间】:2013-03-20 17:07:06
【问题描述】:
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()?必须调用种子才能初始化随机数生成器。
-
不,您不必调用
random.seed,当前系统时间用于在模块首次导入时初始化生成器。 -
你能用散文(不是代码)解释你想要什么吗?
-
这看起来像他想要的:“返回一个包含 n 个小写字母的随机手。至少 n/3 [of] 个手应该是元音。”