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.

On 23/05/2013 23:21, Dave Cragg wrote:
On 23 May 2013, at 21:11, Björnke von Gierke <b...@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.

Cheers
Dave
_______________________________________________
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


_______________________________________________
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