Author Topic: Shuffling Arrays  (Read 2508 times)


  • Full Member
  • ***
  • Posts: 135
  • Scripts for Praise
    • CorpseCorp
Shuffling Arrays
« on: May 22, 2008, 01:07:06 AM »
I needed to shuffle an array so I found this article, but it doesnt work for icechat out of the box. With a few modifications, you have an array shuffler. Note: this function shuffles array items inside the existing array; you may want to make a copy of the array before shuffling.

Code: [Select]
Sub ArrayShuffle(arr)
  Dim index
  Dim newIndex
  Dim firstIndex
  Dim itemCount
  Dim tmpValue
  firstIndex = LBound(arr)
  itemCount = UBound(arr) - LBound(arr) + 1
  For index = UBound(arr) To LBound(arr) + 1 Step -1
    ' evaluate a random index from LBound to INDEX
    newIndex = firstIndex + Int(Rnd * itemCount)
    ' swap the two items
    tmpValue = arr(index)
    arr(index) = arr(newIndex)
    arr(newIndex) = tmpValue
    ' prepare for next iteration
    itemCount = itemCount - 1
End Sub

Now just type
Code: [Select]
ArrayShuffle MyArrayAnd you have one shuffled array.
This message brought to you by the Wonderful Wizard of Oz.