112
import random
x = [1, 2, 3, 4, 5, 6]
random.shuffle(x)
print x

I know how to shuffle a list, but is it possible to shuffle it with a parameter such that the shuffling produces the same result every time?

Something like;

random.shuffle(x,parameter)

and the result is the same for this parameter. Say parameter is 4 and the result is [4, 2, 1, 6, 3, 5] every time.

3
  • 6
    Use the random.seed() function.
    – Tim Peters
    Oct 10, 2013 at 22:02
  • 9
    changing the seed pollutes the randomness of your entire program. do not do it lightly.
    – Eevee
    Oct 10, 2013 at 22:06
  • 7
    @Eevee: Which is why you can create new instances of random.Random and call seed on them (or just pass the seed as an initializer).
    – abarnert
    Oct 10, 2013 at 22:30

4 Answers 4

228

As the documentation explains:

The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state.

So, you can just create your own random.Random instance, with its own seed, which will not affect the global functions at all:

>>> import random
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]
>>> x = [1, 2, 3, 4, 5, 6]
>>> random.Random(4).shuffle(x)
>>> x
[4, 6, 5, 1, 3, 2]

(You can also keep around the Random instance and re-seed it instead of creating new ones over and over; there's not too much difference.)

1
  • 5
    Nice solution! +1 for not affecting the global functions Jan 13, 2017 at 23:19
49

You can set the seed (which accepts the parameter) of your random generator, which will determinize your shuffling method

import random
x = [1, 2, 3, 4, 5, 6]
random.seed(4)
random.shuffle(x)
print x

and the result should be always

[2, 3, 6, 4, 5, 1]

In order to "rerandomize" the rest of the code you can simply reseed your random number generator with system time by running

random.seed()

after your "deterministic" part of code

3
  • 11
    note that this will pollute the results of every function in the random module for the rest of your program. passing the second argument to shuffle will only affect that one call.
    – Eevee
    Oct 10, 2013 at 22:05
  • 1
    Yes, this is an important note, even though you could simply run random.seed() again after shuffle to reinitialize it to the system time.
    – lejlot
    Oct 10, 2013 at 22:06
  • 1
    That's still overly complicated and not very robust. (What if you have multiple threads? What if there's an exception before you get around to re-seed-ing?) This is exactly why you can construct new instances of random.Random, so you don't have to muck with the global instance.
    – abarnert
    Oct 10, 2013 at 22:25
11

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!

-2

From the python reference:

The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random()

You can use a lambda function that always returns the same value as a seed for shuffle:

In [7]: l = [1,2,3,4]
In [8]: random.shuffle(l, lambda: .5)

In [9]: l
Out[9]: [1, 4, 2, 3]

In [10]: l = [1,2,3,4]

In [11]: random.shuffle(l, lambda: .5)

In [12]: l
Out[12]: [1, 4, 2, 3]  # same order as Out[9]
2
  • 2
    It turns out this is a very bad idea and results in an almost unshuffled list for long lists! See my answer for details.
    – Mark
    Apr 16, 2015 at 19:18
  • 2
    This is a terrible answer. Do not use the second argument to random.shuffle() to return a fixed value. You are no longer shuffling, you are producing a bad fixed swap sequence ill suited for real work. Use random.seed() instead before calling random.shuffle() with just one argument. See Python shuffle(): Granularity of its seed numbers / shuffle() result diversity. The function passed in is called more than once, and should produce a new random value each time; a properly seeded RNG will produce the same 'random' sequence for a given seed.
    – Martijn Pieters
    Jan 20, 2018 at 17:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.