相同随机种子的不同实现

发布于 2024-10-13 01:53:59 字数 539 浏览 7 评论 0原文

我有一个包含不同模块的项目。然后我有一个名为 Main.py 的文件,其中包含一些在运行期间调用这些模块的代码。在文件 Main.py 中,我使用以下方法设置随机种子:

random.seed(2)

即使我使用相同的随机种子,从不同运行中获得的输出也不相同。你能告诉我为什么会发生这种情况吗?我的班级中的各个模块使用 random.uniform、random.choice、random.sample 函数。在一处,我还定义了 rnduniform = random.uniform 并使用它。

任何有关如何解决此问题(即能够通过设置随机种子来复制结果)并帮助我理解这一点的帮助将不胜感激。

谢谢。

编辑:已解决。我的错误。

抱歉浪费了您的时间。我更仔细地查看了代码,其中一个使用随机数生成的函数是在其中一个类的 init 方法中调用的。在设置种子之前访问 init 方法。我试图删除该帖子,但无法删除。因此,进行了这次编辑。

I have a project with different modules. Then I have a file called Main.py which has some code that calls these modules during the run. In the file Main.py I set random seed using:

random.seed(2)

The output that I get from different runs is not identical even if I use the same random seed. Could you tell me why this might be happening? The various modules in my class use random.uniform, random.choice, random.sample functions. In one place, I also define rnduniform = random.uniform and use that.

Any help about how to solve this problem (i.e., be able to replicate the result by setting random seed) and help me understand this would be greatly appreciated.

Thank you.

EDIT: Solved. My error.

Sorry for wasting your time. I looked more carefully at the code and one of the functions that uses random number generation was called in an init method of one of the classes. The init method was accessed before the seed was set. I tried to delete the post but I could not. Hence, this edit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2024-10-20 01:53:59

线程安全涉及并发编程 - 或者换句话说,当两个不同的代码路径通过线程同时执行时。作为程序员,一行代码通常是多个单独的操作,因此不同的线程可能会干扰您正在使用的任何变量,或使用中间计算。这将导致很难理解的错误,因为通常你的代码看起来完全没问题。

在这种情况下,他说您使用 random() 的代码和线程中以某种方式使用随机数生成器的其他代码可能会发生冲突,并且不会按预期运行。例如,这些数字可能不再是数学上随机的,或者如果您使用某个基础种子进行初始化,然后期望 random() 在多次调用中返回多个设置值,那么这些数字可能不会是您期望被退回的人。在使用非线程安全函数的最坏情况下,您可能会遇到严重的异常和/或崩溃,因为该函数并非设计为同时在多个线程中使用。

另请参阅有关线程安全的维基百科主题

Thread safety deals with concurrent programming - or in other words, when you two different codepaths executing at the same time through means of threading. As something that might be a single line of code to you as a programmer are usually a multitude of seperate actions, a different thread might interfere with whatever variables you are using, or use intermediate calculations. This will cause very hard to understand bugs because usually your code will seem completely fine.

In this case, he is saying that your code using random() and other code in a thread that is somehow using the random number generator might conflict and not behave as expected. For example, the numbers might not be as mathematically random any longer, or if you initialize with a certain base seed and then expect random() return a number of set values over multiple calls, those numbers may not be the ones you expect to be returned. In the very worst case of using non-thread-safe functions, you might end up with harsh exceptions and/or crashes since the function is not designed to be used in multiple threads at the same time.

Also see the Wikipedia topic on Thread safety.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文