I am involved in a python application that will be running on Mac and Windows, packed into executables with py2app and py2exe. It is important that all apps generate the same random numbers at all times. So the random module always gets the same seed.
However, the outcomes of random.shuffle() seem to be very different on Windows an Mac.
Is there any way to make sure the same random numbe开发者_C百科rs are always generated?
Code is very unexciting:
import random
random.seed("ladygaga")
swaplist_odd = [n for n in range(len(clipboard)) if n % 2 != 0]
swaplist_even = [n for n in range(len(clipboard)) if n % 2 == 0]
random.shuffle(swaplist_odd)
random.shuffle(swaplist_even)
The idea is to shuffle a buffer, and be able to recreate the original buffer by shuffling again.
If the process stays within one operating system, there is no problem. As soon as the buffer files are copied to another operating system, the re-creation of the original fails.
Is it possible that you're using two different versions of Python?
According to the documentation, Python moved from using the Wichmann-Hill algorithm to using the Mersenne Twister to generate pseudo-random numbers starting at 2.3.
Python 2.x and Python 3.x seem to have incompatible random number generators and return different result even when seeded with the same number.
For me, Python 2.7.16 on MacOS returns the same as Python 2.7.12 on Ubuntu.
Python 3.7.3 on MacOS returns the same as Python 3.5.2 on Ubuntu.
Moreover, hash of a string can be different on different Python installations, so you should be using a numeric seed instead of ladygaga
.
精彩评论