mercredi 24 février 2016

(C#) How to fill a 2D array with 2 different numbers evenly, but randomly?

For a randomized tic-tac-toe game simulator, I need to utilize a 2D array to simulate a round between 2 players. To fill the array, I've been using a simple randomizer to generate a random number between 0 and 1.

    //create 2D array
  const int ROWS = 3;
  const int COLS = 3;
  int[,] tictactoeArray = new int[ROWS, COLS];
   //create random variable
  Random rand = new Random();

   //method to fill array with random values between 0 and 1.
  private void RandomizeArray(ref int[,] iArray)
  {
      for (int row = 0; row < ROWS; row++)
      {
          for (int col = 0; col < COLS; col++)
          {
              tictactoeArray[row, col] = rand.Next(2);
          }
      }
  }

But when I use a random number generator like this, I occasionally end up with an impossible combination of values in relation to tic-tac-toe. As in the value 1 will occur 6 or more times, and make the game unfair for the other "player". I've done extensive searching, but haven't found a way to (more or less) evenly distribute the 2 values. Any suggestions?




Aucun commentaire:

Enregistrer un commentaire