From: Uwe Schmitt Date: 2005-12-07T01:03:29+09:00 Subject: Re: Shuffling an array, sort_by{rand}'s bias (was Re: need some Ruby || > || arr.sort_by{rand} || > that is not equivalent to my solution. || > the python equivalent to yours is || > arr.sort(key = lambda x: random()) || > || > In Python it is quite easy by using list comprehensions... || > || What does that look like in Python? || > || > from random import random || > || > def shuffle(a): || > b = [ (random(), i) for i in a] || > b.sort() || > return [ x[1] for x in b ] || > || > print shuffle(range(10)) || I would have thought those two functions do the same thing || with the exception that shuffle does not sort in place. || I'm probably missing something obvious. Could you explain || the difference between the two versions? If you use random as sorting key, the result of a<=>b will differ each time you evaluate a<=>b. In my case I attach a random number to each item, then I sort my list according to this number and remove this 'decoration' afterwards. Pythonprogrammers call this pattern "decorate-sort-undecorate". As I do not now anything about the ruby interanls of sort, I prefer my version. Greetings, Uwe.