IceChat Forums

IceChat 7 => IceChat 7 Scripts => Topic started by: DarkStar on May 22, 2008, 01:07:06 AM

Title: Shuffling Arrays
Post by: DarkStar on May 22, 2008, 01:07:06 AM
I needed to shuffle an array so I found this article (http://www.devx.com/vb2themax/Tip/19011), 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)
  Randomize
  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
  Next
End Sub

Now just type
Code: [Select]
ArrayShuffle MyArrayAnd you have one shuffled array.