random.shuffle?
Jake Speed
speed at ?.com
Mon Jul 31 14:46:22 EDT 2000
max at alcyone.com (Erik Max Francis) wrote in
<3985C75F.7030AB6C at alcyone.com>:
>François Pinard wrote:
>
>> def shuffle(items):
>> """Shuffle ITEMS in place. ITEMS should normally be a list."""
>> for top in range(len(items)-1, 0, -1):
>> index = whrandom.randint(0, top)
>> items[index], items[top] = items[top], items[index]
>
>This is a biased shuffling algorithm. When swapping cards, you'd want
>to pick the other card from the whole of the deck, not the deck that
>you've touched so far. If you start with a sorted deck, for instance,
>it's easy to see that cards can't end up in every place; see Knuth.
It's not selecting from the deck touched so far; it's
selecting from the untouched deck -- it's counting backwards.
This is the correct way to do it.
-Speed!
More information about the Python-list
mailing list