简体   繁体   English

使用 Joblib 的随机数生成器

[英]Random number generator using Joblib

I need to generate random number in a function which is paralleled using Joblib.我需要在使用 Joblib 并行的函数中生成随机数。 However, the random number generated from the cores are exactly the same.但是,内核生成的随机数完全相同。

Currently I solved the problem by assigning random seeds for different cores.目前我通过为不同的核心分配随机种子来解决这个问题。 Is there any simple way to solve this problem?有什么简单的方法可以解决这个问题吗?

This is expected, although unfortunate.这是意料之中的,虽然很不幸。

The reason is that joblib (based on the standard multiprocessing Python tool) relies on forking under Unix.原因是 joblib(基于标准的多处理 Python 工具)依赖于 Unix 下的 fork。 Forking creates the exact same processes and thus the same pseudo-random number generation.分叉创建完全相同的进程,因此产生相同的伪随机数。

The right way to solve this problem is to pass to the function that you are calling in parallel a seed for each call, eg a randomly-generated integer.解决这个问题的正确方法是将每个调用的种子传递给您并行调用的函数,例如随机生成的整数。 That seed is then used inside the function to seed the local random number generation.然后在函数内部使用该种子来为本地随机数生成提供种子。

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

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