Shuffling with a fixed value for random
does NOT work well! Example:
from random import shuffle
v = sum([[k] * 100 for k in range(10)], [])
print v[:40]
shuffle(v, random = lambda: 0.7)
print v[:40]
Gives the output:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 8, 0, 0, 9, 0, 0, 0, 9, 0, 0, 8, 0, 0, 7, 0, 0, 0, 9, 0, 0, 7, 0, 0, 8, 0, 0, 0, 7, 0, 0, 7, 0, 0, 8, 0, 0, 0, 9]
It's similar for other seeds - not very random (at first sight anyway... hard to prove). This is because random
is not a seed - it is reused many times. Demonstration:
def rand_tracker():
rand_tracker.count += 1
return random()
rand_tracker.count = 0
shuffle(v, random = rand_tracker)
print 'Random function was called %d times for length %d list.' % (rand_tracker.count, len(v))
Which shows:
Random function was called 999 times for length 1000 list.
What you should do instead is @abarnert's suggestion:
from random import Random
Random(4).shuffle(x)
In that case a fixed value is perfectly fine.
TLDR: Use the answer by @abarnert, do not use a fixed-value function for random
!
random.seed()
function.random.Random
and callseed
on them (or just pass the seed as an initializer).