randomly order a list

Björnke von Gierke bvg at mac.com
Fri May 24 07:00:01 EDT 2013


Seems someone beat us to entering this feature request:

http://quality.runrev.com/show_bug.cgi?id=10919

On 24.05.2013, at 12:04, Dave Cragg wrote:

> 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 <alex at 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 at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


-- 

Use an alternative Dictionary viewer:
http://bjoernke.com/bvgdocu/

Chat with other RunRev developers:
http://bjoernke.com/chatrev/






More information about the use-livecode mailing list