用于单词游戏的随机字符 Python

0 投票
3 回答
2406 浏览
提问于 2025-04-17 19:46
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。我想知道有没有办法让返回的字母组合更随机一些?

3 个回答

0

在这个版本中,你应该有统计上大约三倍于辅音的元音,但具体的数量并没有保证。

import collections
import random

VOWELS = 'aeiou'
CONSONANTS = 'bcdfghjklmnpqrstvwxyz'

def dealHand(n):
 letters = 3 * VOWELS + CONSONANTS 
 collections.Counter(random.sample(letters, n))
1

"返回的字母并不是很随机,有很多重复的字母" - 这是真的吗?

如果你想要得到n个不重复的字母,可以用下面的方式:

from random import shuffle
alphabet = ['a', .., 'z']
shuffle(alphabet)
print(alphabet[:n])

如果n大于字母表的长度,那你无论如何都会得到重复的字母。

1

这里是你算法的一个更简洁的表示方式:

from __future__ import division
from collections import Counter
import random
import string

VOWELS = "aeiou"
CONSONANTS = "".join(set(string.lowercase) - set(VOWELS))

def dealHand(n):
    numVowels = n // 3
    lettersets = [VOWELS] * numVowels + [CONSONANTS] * (n - numVowels)
    return Counter(c
        for letterset in lettersets
        for c in random.choice(letterset)
    )

看起来挺随机的。


接下来提到:“如果我想让字母出现不超过两次,我该怎么做?”

你可以这样做,不过我并不推荐这种方法:

def dealHand2(n):
    while True:
        candidate = dealHand(n)
        if all(v <= 2 for v in candidate.values()):
            return candidate

这是一个无限循环,直到找到一组符合你条件的字母为止。运行时间:不确定。

撰写回答