Shuffle

curry curry at pair.com
Sat Jul 19 00:24:03 EDT 2003


Great example in the How To about shuffling with a random sort! 
Amazing that's even possible. Revolution never fails to bring a sense 
of wonder to programming.

>sort items of myVar numeric \
>by random(the number of items in myVar)

But should we count the number of lines or items first?

Here's a test to perform in a button:

on mouseup
   --build a big list
   repeat with i=1 to 10000
     put i & cr after x
   end repeat
   --time the operation
   put the long sec into t
   --sort the original way
   sort lines of x numeric by random(num of lines in x)
   --record the time
   put the long sec - t into a
   put the long sec into t
   --count first
   put num of lines in x into n
   --use the pre-count for the random
   sort lines of x numeric by random(n)
   put the long sec - t into b
   --show the times
   answer a,b
   --show how much difference
   answer a/b
end mouseup

On my computer the second sort is 28 times faster.

Still, the way it's written in the How To is easy to understand for 
introducing the concept. But for people seriously using it, here you 
go. I just think this is a beautiful example and I have done my own 
clumsy shuffling thing many times; you can bet I'm switching to this 
method!

(And actually, if you really wanted to be perfect, you might improve 
it by multiplying the number of lines or items by a desired amount 
for your random number parameter, so you have less chance of two 
lines having the same random number and (I assume) remaining in the 
order they originally were in the list.)

 From time to time I'm going to see what else is lurking there in How 
To; I think it's not just for newcomers!!! :-)

Curry



More information about the use-livecode mailing list