开发者

Different realizations with the same random seed

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 func开发者_运维问答tions. 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜