Go Pro!

Randomizing an Array of Indices

Reference > Science > Technology > Beginner Programming Tips

In my previous article, I talked about how to Randomize An Array. As I said on that page, the method I gave is certainly not the only one, and I promised to share another method. This method here is slightly more complex, but there are situations where the first method is not satisfactory, and you need the additional feature provided by this new method I'm going to share today. In fact, I had to use this method just a few days ago when I was programming this online word game: Word Grid Puzzle.

Suppose you want to scramble (randomize) an array of values, but you also need to maintain a copy of the array in unscrambled order. One method would be to create a copy of the array, and then scramble the order of the copy. I won't bother showing you this method, but it is a reasonable approach. It should work fine, but if you have a large array, filled with string values, you may be chewing up a lot of memory unnecessarily with that method.

So instead, let's create an array of numeric values (remember, numeric values will likely take up much less space than your array of strings) that represent indices into the array. Then we'll scramble the array of indices, and use that scrambled array to pull values from our original array.

That may sound confusing, but it really isn't too bad, once you see it in action. Let's take a look at some code. If you looked at the previous article, much of this code will look very familiar.

Dim MyArray(9) As String
Dim MyIndexArray(9) As String
Dim I As Integer
Dim Index1 As Integer
Dim Index2 As Integer
Dim Temp As Integer

'Fill The Index Array
For I = 0 To 9
   MyIndexArray(I) = I
Next I

'Scramble the Indices Order
For I = 1 To 20
   Index1 = Int(Rnd * 10)
   Index2 = Int(Rnd * 10)
   While Index1 = Index2
      Index2 = Int(Rnd * 10)
   Temp = MyIndexArray(Index1)
   MyIndexArray(Index1) = MyIndexArray(Index2)
   MyIndexArray(Index2) = Temp
Next I

As I said, much of that code will look familiar if you read the previous article, so I won't talk through the second part of the code. Let's look at the first part. I've taken MyIndexArray() and filled it with the numbers 0 through 9. Now, if I wanted to refer to MyArray(5), there are two ways I could do it:

'First Method
MyArray(5) = "Hello"

'Second Method
MyArray(MyIndexArray(5)) = "Hello"

It may seem pointless to refer to an element of MyArray using the second method, but you have to agree it would work, since MyIndexArray(5) = 5!

If you followed that, now you can take a look at the rest of the code, and see that I have scrambled the values of MyIndexArray(). So now we don't know what MyIndexArray(5) equals. It might be five, but it might be zero. Or seven. It's randomized.

Now when I refer to MyArray(MyIndexArray(5)), am I referring to MyArray(5)? Not necessarily! I'm referring to some randomly selected member of the array MyArray(). Without having to create a second string array, I can refer to either the unscrambled string array, or the scrambled string array:

Dim I As Integer
'Display Unscrambled Array
For I = 0 To 9
   Print MyArray(I)
Next I

'Display Scrambled Array
For I = 0 To 9
   Print MyArray(MyIndexArray(I))
Next I


At the beginning of this article, I mentioned that one method of scrambling the array while keeping the order of the original was to create a copy of the array, and then scramble the copy. Write code to use this method on an array of strings.
Assign this reference page
Click here to assign this reference page to your students.
Randomizing an ArrayRandomizing an Array
Generating Random PasswordsGenerating Random Passwords

Ask Professor Puzzler

Do you have a question you would like to ask Professor Puzzler? Click here to ask your question!
Get a FREE Pro-Membership!
Educators can get a free membership simply by sharing an original lesson plan on our Articles for Educators page!

Like us on Facebook to get updates about new resources
Pro Membership