Random sort demonstration

Richard Gaskin ambassador at fourthworld.com
Thu May 23 09:51:12 EDT 2013

Dick Kriesel wrote:

 > command shuffle @rLines
 >    sort rLines by random( 4294967295 ) -- note:  2^32-1
 > end shuffle

Wouldn't that put the probability of sorting bottom-to-top unusually 
high, since there the odds are 4294967292-to-1 that the sort integer 
will exceed the number of lines in the list?

Chris Sheffield originally wrote:

 > I have a list of three words that I need to be randomly sorted.
 > To start with, the first word is the correct answer to a question.
 > I want to re-order the list so that the correct answer may be the
 > second or third word, and not necessarily the first.

If we were to do this manually, we'd do something like:

1. Put a random line out of three lines into a new list.
    This leaves us with two remaining lines, so:
2. Put a random line out of two lines after the new list.
    This leaves us with just one so:
3. Put the remaining line at the bottom of the new list.

On a field containing lines with "rat", "cat", and "bat", this simple 
script provides a reasonable distribution of results:

on mouseUp
    put fld 1 into tList
    put random(3) into n
    put line n of tList into tNewList
    delete line n of tList
    put random(2) into n
    put cr& line n of tList after tNewList
    delete line n of tList
    put cr& line 1 of tList after tNewList
    put tNewList into fld 2
end mouseUp

I wouldn't use such a crude method for a long list, but for just three 
lines it seems to get the job done.

Did I miss something in the original problem statement?

  Richard Gaskin
  Fourth World
  LiveCode training and consulting: http://www.fourthworld.com
  Webzine for LiveCode developers: http://www.LiveCodeJournal.com
  Follow me on Twitter:  http://twitter.com/FourthWorldSys

More information about the use-livecode mailing list