Randomizing a List

Date:    Fri, 14 Jul 1995 17:34:00 N
From:    Mike Nelson <miken@TERABYTE.CO.NZ>
Subject: Randomising lists
I saw people were thinking about using recursion and other means to randomise the positions of items in a list. The routine I saw took one list and created a new list. I thought it would be better to just swap the items in the one list. (So you don't need to create a new list).

This is my one:

on jumbleList anyList
  -- randomises the order of elements in a list
  repeat with pos = 1 to count( anyList )
    -- swap this element with a random one
    set temp = getAt( anyList, pos )
    set swappos = random( count( anyList ) )
    setAt anyList, pos, getAt( anyList, swappos )
    setAt anyList, swappos, temp
  end repeat
  -- return the result list
  return anyList
end

I also find this useful for adding an element of randomness to multichoice selections:
on jumbleSprites startChannel, endChannel
  -- randomise sprite positions (makes sprites into puppets)
  repeat with ch = startChannel to endChannel
    -- make it controllable from lingo (if not already)
    puppetSprite ch, true
    -- swap location with another sprite
    set swapch = random( endChannel - startChannel ) + startChannel
    set tempH = the locH of sprite ch
    set tempV = the locV of sprite ch
    set the locH of sprite ch to the locH of sprite swapCh
    set the locV of sprite ch to the locV of sprite swapCh
    set the locH of sprite swapCh to tempH
    set the locV of sprite swapCh to tempV
  end repeat
end
Mike Nelson
TERABYTE INTERACTIVE
New Zealand
:)