Implementaion of random.shuffle
shabda.raaj at gmail.com
Mon Jul 16 14:10:01 CEST 2007
I was going through the docs for
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
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,
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)
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list