Search:
Home Math Games Word Games Strategy Reference Junior Games Daily Puzzles Printables Problems Members

Home Reference Programming Tips

Each page contains a helpful programming tip and exercises which encourage beginners to use what they've learned in a different situation.
Index Previous    Next    Teachers    About   

Randomizing An Array Of Indices

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)
   Wend
   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

Other Scenarios

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.

Index Previous    Next    Teachers    About   

"Beginner Programming Tips and Tricks" is written by Douglas Twitchell, and hosted at The Problem Site.

Contents copyright 2005 by Douglas Twitchell. Contents of this page may not be reproduced without permission of the author. For information on using this site in a classroom situation, please visit the Teachers page.

More programming information and other tips can be found at Virtu Software's Ask Doug site.

 



 
Search For More Resources Search For More Educational Resources

 

Top Games On This Site

 
Adders!
Scramble
Entrapment
Secret Word
Trio Match
 

Just a few of the educational game resources available on The Problem Site!

 
 






Home       All Games       Problems       Contact       Site History       Privacy Policy       Member Page