In Python 3.2, the random module was reorganized a bit to make the output uniform across architectures (given the same seed), see issue # 7889 , The shuffle() method was switched to using Random._randbelow() .
However, the _randbelow() method _randbelow() also been adjusted, so just copying version 3.5 of shuffle() not enough to fix it.
However, if you pass in your own random() function, the implementation in Python 3.5 will not change from version 2.7 and, thus, will circumvent this limitation:
random.shuffle(l, random.random)
Please note, however, that you are now exposed to the old 32-bit and 64-bit architecture differences that you tried to solve. # /
Ignoring a few optimizations and special cases, if you include _randbelow() , version 3.5 can be referred to as:
import random import sys if sys.version_info >= (3, 2): newshuffle = random.shuffle else: try: xrange except NameError: xrange = range def newshuffle(x): def _randbelow(n): "Return a random int in the range [0,n). Raises ValueError if n==0." getrandbits = random.getrandbits k = n.bit_length()
which gives you the same result on 2.7 as 3.5:
>>> random.seed(42) >>> print(random.random()) 0.639426798458 >>> l = list(range(20)) >>> newshuffle(l) >>> print(l) [3, 5, 2, 15, 9, 12, 16, 19, 6, 13, 18, 14, 10, 1, 11, 4, 17, 7, 8, 0]