如何使random.shuffle真正随机?

时间:2021-04-05 21:22:41

So I have this list:

所以我有这个清单:

words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]

And I have this which shuffles it and displays it on a label:

我有这个将它洗牌并将其显示在标签上:

entry.delete(0, tkinter.END)
random.shuffle(words)
label.config(text=str(words[1]))
timeLabel.config(text="Time: " + str(time_score)+ "s")

I'd like to know how to make this shuffle truly random because it feels like I'm getting the same word over and over again despite there being a lot in the list. Maybe there is a way to remove a value from the list once it's been taken from it so it can't be shown again?

我想知道如何使这个随机播放真正随机,因为尽管列表中有很多内容,我觉得我一遍又一遍地听到同一个词。也许有一种方法可以从列表中删除一个值,因此无法再显示它?

2 个解决方案

#1


3  

You can try:

你可以试试:

import random
random_word = random.choice(words)

to choose random word from your array. Then you can do:

从数组中选择随机单词。然后你可以这样做:

words.remove(random_word)

to remove this randomly selected word from array to avoid getting it again if you want to get next random word.

从数组中删除这个随机选择的单词,以避免再次获取它,如果你想获得下一个随机单词。

Edit - answer to the comment

编辑 - 回答评论

When I run this code below I'm getting different word every time. Is this the logic you are looking for?

当我在下面运行这段代码时,我每次都会得到不同的词。这是你正在寻找的逻辑吗?

words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]

import random
while words:
    random_word = random.choice(words)
    print(random_word)
    words.remove(random_word)

#2


4  

A variation @grael's solution is to shuffle, and then use list.pop to remove items. There's no need to shuffle your list more than once.

变种@grael的解决方案是随机播放,然后使用list.pop删除项目。没有必要多次洗牌。

In [54]: words = ['foo','bar','baz','quux','bizby','squiggle']

In [55]: random.shuffle(words)

In [56]: while words: print(words.pop())
bizby
bar
foo
baz
squiggle
quux

#1


3  

You can try:

你可以试试:

import random
random_word = random.choice(words)

to choose random word from your array. Then you can do:

从数组中选择随机单词。然后你可以这样做:

words.remove(random_word)

to remove this randomly selected word from array to avoid getting it again if you want to get next random word.

从数组中删除这个随机选择的单词,以避免再次获取它,如果你想获得下一个随机单词。

Edit - answer to the comment

编辑 - 回答评论

When I run this code below I'm getting different word every time. Is this the logic you are looking for?

当我在下面运行这段代码时,我每次都会得到不同的词。这是你正在寻找的逻辑吗?

words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]

import random
while words:
    random_word = random.choice(words)
    print(random_word)
    words.remove(random_word)

#2


4  

A variation @grael's solution is to shuffle, and then use list.pop to remove items. There's no need to shuffle your list more than once.

变种@grael的解决方案是随机播放,然后使用list.pop删除项目。没有必要多次洗牌。

In [54]: words = ['foo','bar','baz','quux','bizby','squiggle']

In [55]: random.shuffle(words)

In [56]: while words: print(words.pop())
bizby
bar
foo
baz
squiggle
quux