Unsorting(randomizing) a sequence

Stephan Houben stephan at pcrm.win.tue.nl
Tue Aug 17 04:06:25 EDT 1999


Ole-Kristian Villaboe <olevi at my-deja.com> writes:

> Is there a command to do this? (Can't find one but...)
> I've been writing an algorithm myself, but it's incredibly slow and I
> would like something quicker (of course :) )

This works for me:

import random

def randomize(l):
    length = len(l)
    for i in range(length):
        j = rand.randrange(i, length)
        l[i], l[j] = l[j], l[i]

Note that this randomizes the list in-place.

Greetings,

Stephan




More information about the Python-list mailing list