Okay, so I have two arrays:

public int[] start = new int[21] { 0, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 };
        public int[] end = new int[21] { 0, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14 }

I'm supposed to pick a random value from both arrays, and stick it in a 2D array. So, here is my code for that:

for (int i = 0; i < 21; i++)
            {
                Random ranNum = new Random();
                int genX = 0;
                int genY = 5;
                int indx = 1;

                while (Board[genX, genY].rank > -1)
                {
                    genX = ranNum.Next(0, 9);
                    genY = ranNum.Next(5, 8);
                }
                while (startBoard[indx] == -1)
                {
                    indx = ranNum.Next(0, 21);
                }
                if (gameBoard[genX, genY].rank == -1)
                {
                    Board[genX, genY].rank = start [indx];
                    startBoard[indx] = -1;
                }
                while (gameBoard[genX, genY].rank > -1)
                {
                    genX = ranNum.Next(0, 9);
                    genY = ranNum.Next(0, 3);
                }
                while (enemyBoard[indx] == -1)
                {
                    indx = ranNum.Next(0, 21);
                }
                if (Board[genX, genY].rank == -1)
                {
                    Board[genX, genY].rank = end[indx];
                    enemyBoard[indx] = -1;
                }                
            }

Actually, this works fine. ONCE. Board is a custom object. Basically, that code randomly picks an item inside one of the arrays, randomly generates an X and Y index, and places it in the 2D array. It also replaces the item taken from the (previous 1D) array by a -1, to signify that there is nothing in that index, and thus, won't get picked.

As well, the 2D array is initially filled with -1's, to signify that there is nothing in them. If there is anything in it, the code will randomize another set of X and Y. Take note that there are different min / max values for the random for the "start" and "end" array items. This is so that they end up on the upper and lower end of the 2D array (the middle values stays at "-1").

Here's my problem. I have a code to reset that. Basically, it just restores back the arrays to before it had -1's, and erases all values in the 2D array and replaces it with -1's.

But, say, I run that code above. I am not satisfied with the placement. I reset the arrays, and run that code again.

At which point, the entire program stops running.

Could I have some help on this? Any ideas why it happens?

Thanks in advanced.

Hm. You might be accidentally indexing outside of the bounds of one of your arrays. It sounds odd that it would just stop working... is this a release build? I'd expect to see some sort of exception message. Are you running it from within an IDE?

I'm running it using Visual Studio 2005. I just press Start Debugging. There is no error message of any sort, and no index out of bounds error. Like I said, it works, but only once. But after restoring all the arrays to their previous condition and attempting it again, it just stops working.

Strange, however, that removing the secondary While loops concerning the "end" array allows the program to continue as normal, even if I clear the arrays and repeat the process.

EDIT: "StartBoard" in the for loop is actually the "start" array, and "EnemyBoard" is actually the "end" array. The names match in the program, it's just a typo I wasn't able to correct in here, so the problem still stands.

Well, I've rewritten the code, mostly I just removed the "Random ranNum = new Random();" out of the loop.

I also checked the Reset function for any possible errors.

Though I have no idea how this happened, it seems to work fine now. Thanks for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.