On Sun, Apr 13, 2008 at 5:19 PM, Chuck Pelto <[log in to unmask]> wrote: > > on shuffle (aList) > > set length to count aList > > repeat with i from count to 2 by -1 -- Correcting myself: the repeat line should read "length", not "count" -- > Truly elegant code, there. Thanks, but I was only going for simple. :) > But for us neophytes who are not all that confident with such higher forms of math.... ? What higher forms of math? The math is the same. All I did was take advantage of a couple AppleScript constructs that cut down on the amount of code required. > > A common error is to replace the random number bounds to (1..i-1), > > eliminating the if conditional and guaranteeing a swap on each step, > > but then the distribution of resulting permutations is no longer even. > > > > Thanks for pointing out my potential error. I didn't. Your code was fine. I was pointing out an error that happens when someone starts down the simplification route, like I did, and then goes a little too far. > I've changed the code to read as follows.... Nope. The new code now *exhibits* the problem I mentioned, since it doesn't move on down the list until an exchange has been made - it's just a less efficient version of getting a random number between 1 and itCount-1 instead of itCount in the first place. That changes the result - there is a whole class of shuffled lists that it will never return (for instance, it's impossible for the list to come out in the same order it went in). My code is a complete solution. No need to change it, other than to correct my syntax errors. Here's the corrected version: on shuffle(aList) set listSize to count aList repeat with i from listSize to 2 by -1 set j to random number from 1 to i if j is not i then tell aList to set {item i, item j} to {item j, item i} end if end repeat end shuffle -- Mark J. Reed <[log in to unmask]>