Search code examples
pythonrandomchars

random chars for word game python


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

This function is part of a word game I had to make, it was included in some helper functions to help get started, my problem is that the letters it returns are not very random, there are a lot of repeated letters like: a a c c b e e g j j m m m o o r t v y x, I am just wondering if it is possible to get a more random set of chars?


Solution

  • Here is a more compact representation of your algorithm:

    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)
        )
    

    Seems random enough.


    And later: " if I wanted letters to appear no more than twice, how could I achieve that?"

    Well, you could do this, but I am not recommending this:

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

    This is an infinite loop until it finds a set of letters that satisfies your condition. Running time: uncertain.