Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:23:41+00:00 2026-05-26T15:23:41+00:00

I have 2 one dimensional arrays, containing the exact same values: public int[] start

  • 0

I have 2 one dimensional arrays, containing the exact same values:

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 };

Below is the code I use to generate a random index, and grab the element from the array with said index, and stick it in a 2D array, with randomly generated X and Y index.

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

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

Basically, it takes a value from the “start” and “end” arrays, replaces them with a ‘-1’ (so they don’t get picked again), scans the 2D array for ‘-1s’ (so it doesn’t place the number in a location that already has one), and places it there.

Notice that the min and max value for the random are different for the “start” and “end” arrays. This is to ensure that they end up far away from each other, on the other side of the 2D array.

This code works. Now, I have a code that resets all the variables back to their previous state. A reset function which also works. See, if the user is not content with the random placement, they can reset the arrays, and randomize again.

At which point the program simply hangs / locks up. No error, no messages, it just stops working. Please, could you share any ideas on how / why this happens?

Note: If I remove the entire while loop concerning the “end” array, the program can randomize and reset all it wants.

Anyway, the code for the reset:

int resVal = 0;
            for (int i = 0; i < 21; i++)
            {
                startBoard[i] = resVal;
                enemyBoard[i] = resVal;
                if (i == 0)
                    resVal++;
                else if (i >= 6 && i < 19)
                    resVal++;
            }

for (int y = 0; y < 8; y++)
            {
                for (int x = 0; x < 9; x++)
                {
                    gameBoard[x, y] = new classPiece();
                    gameBoard[x, y].rank = -1;
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:23:42+00:00Added an answer on May 26, 2026 at 3:23 pm

    Move Random ranNum = new Random(); out of the cycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which needs to turn many large one-dimensional numpy arrays of
Let's say I have 2 2-dimensional arrays: int a1[][2] = { {1,2}, {3,4}, {5,6}
I have three one-dimensional arrays. The task is to store the numbers which exist
I'm working with VB.Net, and have two one-dimensional Arrays. Is there an Inbuilt function
I have a predetermined amount of one-dimensional double arrays and want to write each
I have three single-dimensional arrays and I need to combine them into one 3-dimensional
I have a one-dimensional array of strings in JavaScript that I'd like to turn
I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two
I have a multi-dimensional array, no problem. How do I interrogator one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.