6

Following is from python website, about

random.shuffle(x[, random])

Shuffle the sequence x in place. 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().

Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.

If I want to repeat getting a random permutation of ['a'..'k'], it seems shuffle will NOT give me the randomness. Is my understanding right?

Thank you!

Community
  • 1
  • 1
Bill Rong
  • 353
  • 4
  • 12
  • 2
    possible duplicate of [Maximal Length of List to Shuffle with Python random.shuffle?](http://stackoverflow.com/questions/3062741/maximal-length-of-list-to-shuffle-with-python-random-shuffle) – ire_and_curses Jul 01 '10 at 17:43
  • thank you Chris and SilentGhost for the quick & detailed explanation! – Bill Rong Jul 01 '10 at 17:56

2 Answers2

6

You don't have anything to worry about. While under len(x) is under 2000, random.shuffle should work just fine.

Community
  • 1
  • 1
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
6

For a sequence of length 11, there are 11! or 39,916,800 (~ 225.3) possible permutations. For the Mersienne Twister (Python's random algorithm) the period is 219937 − 1. In other words, you'll be fine.

Chris B.
  • 85,731
  • 25
  • 98
  • 139