How to randomize an array in

Do you want to randomize the items in an array with Add these 2 functions below to your code to get it working. The reason there’s 2 functions you must add is because the first function is using the second function to make it all work.

To use it, you just type: Shuffle(YourArray, 5)

The number is just how many times it’s going to shuffle the array, you can use whatever you want here.

Here is the code for the 2 functions:

Public Function Shuffle(ByRef arrayToBeShuffled As Array, ByVal numberOfTimesToShuffle As Integer)
        Dim rndPosition As New Random(DateTime.Now.Millisecond)
        For i As Integer = 1 To numberOfTimesToShuffle
            For i2 As Integer = 1 To arrayToBeShuffled.Length
                Swap(arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)), arrayToBeShuffled(rndPosition.Next(0, arrayToBeShuffled.Length)))
            Next i2
        Next i
        Return ("")
End Function

Public Function Swap(ByRef arg1 As Object, ByRef arg2 As Object)
        Dim strTemp As String
        strTemp = arg1
        arg1 = arg2
        arg2 = strTemp
        Return ("")
End Function

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.