I was going through the docs for <a href="http://docs.python.org/lib/module-random.html">module-random</a> And I came through this,<br> <table cellpadding="0" cellspacing="0"><tbody><tr valign="baseline"><td><b><tt id="l2h-1035" id="l2h-1035" class="function">
shuffle</tt></b>(</td>
<td><var>x</var><big>[</big><var>, random</var><big>]</big><var></var>)</td></tr></tbody></table><dl><dd><br></dd><dd>
Shuffle the sequence <var>x</var> in place.
The optional argument <var>random</var> is a 0-argument function
returning a random float in [0.0, 1.0); by default, this is the
function <tt class="function">random()</tt>.
<p>
Note that for even rather small <code>len(<var>x</var>)</code>, the total
number of permutations of <var>x</var> is larger than the period of most
random number generators; this implies that most permutations of a
long sequence can never be generated.</p></dd></dl>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 <a href="http://en.wikipedia.org/wiki/Knuth_shuffle#Shuffling_algorithms">
knuth shuffle</a> 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.<br><br>A quick first draft of implemetation might be,
<br><br>import random<br><br>def shuffle(list_):<br> for i in range(len(list_)):<br> j = random.randint(i, len(list_)-1)<br> list_[i], list_[j] = list_[j], list_[i]<br>if __name__ == '__main__':<br>
a = range(100)<br> shuffle(a)<br> print a<br><br>This is my first post to the list, I am not sure if this is the correct place to ask these questions. Please advise if this is not the correct place.<br><br>--Shabda<br>
<br>