random shuffles

Paul Rubin http
Fri Jul 21 11:22:16 EDT 2006


Boris Borcic <bborcic at gmail.com> writes:
> x.sort(cmp = lambda x,y : cmp(random.random(),0.5))
> pick a random shuffle of x with uniform distribution ?

You really can't assume anything like that.  Sorting assumes an order
relation on the items being sorted, which means if a < b and b < c,
then a < c.  If your comparison operation doesn't preserve that
property then the sort algorithm could do anything, including looping
forever or crashing.



More information about the Python-list mailing list