randomly order a list

Vokey, John vokey at uleth.ca
Thu May 23 11:10:16 EDT 2013

Shuffling is more complicated than commonly thought.  A properly uniform shuffle is given as follows:

function scramble x -- scramble lines or items in a row
  put the number of lines of x into z
  if z = 1 then put the number of items of x into n
  else put the number of lines of x into n
  repeat with i=n down to 2
    put random(i) into k
    if z=1 then put item k of x into t
    else put line k of x into t
    if z=1 then put item i of x into item k of x
    else put line i of x into line k of x
    if z=1 then put t into item i of x
    else put t into line i of x
  end repeat
  return x
end scramble

On 2013-05-23, at 4:56 AM, use-livecode-request at lists.runrev.com wrote:

> Good idea, Jacques.  How about this?
> command shuffle @rLines
>    sort rLines by random( 4294967295 ) -- note:  2^32-1
> end shuffle
> -- Dick

Please avoid sending me Word or PowerPoint attachments.
See <http://www.gnu.org/philosophy/no-word-attachments.html>

-Dr. John R. Vokey

More information about the use-livecode mailing list