简体   繁体   English

Python random.choice 总是选择第一个值

[英]Python random.choice always selects first value

AUDIO_FILES = [
    os.path.join(settings.AUDIO_FILES_DIR, "1.mp3"),
    os.path.join(settings.AUDIO_FILES_DIR, "2.mp3"),
    os.path.join(settings.AUDIO_FILES_DIR, "3.mp3"),
    os.path.join(settings.AUDIO_FILES_DIR, "4.mp3"),
    os.path.join(settings.AUDIO_FILES_DIR, "5.mp3"),
]

def _get_audio_clip():
    audio_file = random.choice(AUDIO_FILES)
    audio_clip = AudioFileClip(audio_file)
    return audio_clip

_get_audio_clip()

Can anyone point out why random.choice always returns first value of the list in this piece of code?谁能指出为什么 random.choice 总是在这段代码中返回列表的第一个值?

This answered my question. 回答了我的问题。 Quoting the answer:引用答案:

What happens is that on Unix every worker process inherits the same state of the random number generator from the parent process.发生的情况是,在 Unix 上,每个工作进程都从父进程继承随机数生成器的相同状态。 This is why they generate identical pseudo-random sequences.这就是为什么它们生成相同的伪随机序列。

Using random.seed() in the worker process solved the problem.在工作进程中使用 random.seed() 解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM