ArrayShuffle – Randomize the order of elements in an array

ArrayShuffle – Randomize the order of elements in an array

' Shuffle the elements of an array of any type' (it doesn't work with arrays of objects or UDT)Sub ArrayShuffle(arr As Variant)    Dim index As Long    Dim newIndex As Long    Dim firstIndex As Long    Dim itemCount As Long    Dim tmpValue As Variant        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    NextEnd Sub

See also  How AI Art Generators Can Save Time and Money for Startups

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist