# Implementaion of random.shuffle

shabda raaj shabda.raaj at gmail.com
Mon Jul 16 14:10:01 CEST 2007

```I was going through the docs for
module-random<http://docs.python.org/lib/module-random.html>
And I came through this,
*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.
Why would we need to generate the total number of permutations for the list
while trying to shuffle it? Should not we just use a knuth
shuffle<http://en.wikipedia.org/wiki/Knuth_shuffle#Shuffling_algorithms>which
does not require us to generate the total number of permutations. As
long as len(x) is smaller than period of random number generator, a
permutation can be generated.

A quick first draft of implemetation  might be,

import random

def shuffle(list_):
for i in range(len(list_)):
j = random.randint(i, len(list_)-1)
list_[i], list_[j] = list_[j], list_[i]
if __name__ == '__main__':
a = range(100)
shuffle(a)
print a

This is my first post to the list, I am not sure if this is the correct