Shuffle list

From Director Online Wiki
Jump to: navigation, search

shuffles all values in a list around randomly.

on shuffleList  _input
  _temp = _input.duplicate()
  _entries = _temp.count()
  newlist = []
  repeat with i = 1 to _entries
    RandomEntry = random(_temp.count())
    add newlist, _temp[RandomEntry]
    _temp.deleteat(RandomEntry)
  end repeat
  return newlist
end

Here's an alternative version that randomizes a linear list or a property list:

-- by Josh Chunick
on shuffleList _list
  if not listP(_list) then return VOID
  _list = _list.duplicate()
  if ilk(_list) = #proplist then 
    isPropList = TRUE
    rndList = [:]
  else
    isPropList = FALSE
    rndList = []
  end if
  cnt = _list.count
  repeat with i = 1 to cnt
    rndNum = random(_list.count)
    if isPropList then
      rndList.addProp(_list.getPropAt(rndNum), _list[rndNum])
    else
      rndList.add(_list[rndNum])
    end if
    _list.deleteAt(rndNum)
  end repeat
  return rndList
end