Nice one, Alex. I spent an hour convincing myself that the sA array could 
contain duplicate elements after the loop, until the penny dropped. Remind me 
never to play cards if you're shuffling. :-)

Dave

On 24 May 2013, at 00:41, Alex Tweedly <a...@tweedly.net> wrote:

> 
> Yes, that's a good shuffle for small data, but a bit slow for larger data 
> sets. I dug out an old function I wrote a few years ago (and converted it to 
> LC); this would be faster for large data sets (time taken grows linearly 
> rather than by the square of the number of lines).
> 
> local sA, sIndex  -- filled with random shuffle index, returned one-by-one 
> from fn calls
> 
> function shuffleLines pSource
>   put empty into tNew
>   put the number of lines of pSource into tNum
>   -- fill an array with 'self' numbers
>   repeat with i = 1 to tNum
>      put i into tA[i]
>   end repeat
> 
>   repeat with n = tNum down to 1
>      put random(n) into tRand
>      put tA[tRand] into sA[n]
>      put tA[n] into tA[tRand]
>   end repeat
>   put 0 into sIndex
>   sort lines of pSource by _shuffle()
>   return pSource
> end shuffleLines
> 
> function _shuffle
>   add 1 to sIndex
>   return sA[sIndex]
> end _shuffle
> 
> -- Alex.
> 


_______________________________________________
use-livecode mailing list
use-livecode@lists.runrev.com
Please visit this url to subscribe, unsubscribe and manage your subscription 
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

Reply via email to