I am writing a hangman game in which the program creates the ‘secret word’ by randomly choosing a topic and then a list of words to present to the player. The same words crop up repeatedly. I realise that random is not truly random, but I wonder if there is a way to make it more random than it is.
Can you share the code you are using?
def choose_topic(self):
topic = random.choice(list(self.topics.keys()))
print(“Topic:”, topic)
return topic
def choose_secret_word(self):
word_list = self.topics[self.topic]
secret_word = random.choice(word_list).upper()
print(“Secret word:”, secret_word)
return secret_word
The print statements are just for debugging.
That’s generally how I would do it. It is pseudo-random and the larger the Sequence is the more random it will appear. You can also use secrets.choice instead of random.choice which I believe is more random.
You could shuffle the list of words and select them in the order they end up, until you run out of words when you could shuffle them again.
Thank you Dan. I will give that a try.
Thank you Franklin. I will try that.
How big is your topic/word list? If it’s small enough you should expect to see repeating results after a few times. (RANDOM.ORG - Frequently Asked Questions (FAQ), Online runner) If you’d rather see a different word each time I think shuffling is the right way to go.
Thank you.
I have 6 lists each with a different topic. The shortest list is 34 words, the longest 78. I am now trying it with secrets and shuffling.