randomly order a list

Dave Cragg dave.cragg at lacscentre.co.uk
Thu May 23 18:21:13 EDT 2013

On 23 May 2013, at 21:11, Björnke von Gierke <bvg at mac.com> wrote:

> Yes, that is why I myself lean towards a feature request. For example the following line could tell the engine to make a unique random number for each of the supplied lines, to not have the problem with lines that come first getting a higher probability:
> sort theData by random(the number of lines in theData) of each

If we're going for a feature request, I'd suggest a new function "shuffle". It's more descriptive than 'sort'. (We're trying to do the opposite of sorting.)

I'm also uncomfortable with using arbitrary high numbers. (How random should it be?) When i've done this before, I've used the analogy of pulling names out of a hat. So a custom function like this (for shuffling lines) rather than use sort.

function shuffleLines pSource
   put empty into tNew
   put the number of lines of pSource into tNum
   repeat with n = tNum down to 1 
      put random(n) into tRand
      put line tRand of pSource & cr after tNew
      delete line tRand of pSource
   end repeat
   return line 1 to -1 of tNew
end shuffleLines

Probably a little slow for large lists, but for things like shuffling answer choices in a test, it's good enough.


More information about the use-livecode mailing list