如何让 random.shuffle 真正随机?

How to make random.shuffle truly random?

所以我有这个列表:

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"]

我有这个可以洗牌并显示在标签上:

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

我想知道如何让这种随机播放真正随机,因为尽管列表中有很多词,但感觉就像我一遍又一遍地听到同一个词。 也许有一种方法可以从列表中删除某个值,以便它不能再次显示?

你可以试试:

import random
random_word = random.choice(words)

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

words.remove(random_word)

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

编辑 - 回复评论

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

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)

@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