This is my relatively simple code. Its just part of a memory matching game.
I've looked but I cannot figure out why it won't switch the values in each of the array indexes.
I would appreciate anyone helping me out here with this.

** Turns out the problem is that the random number generator returns the same value 4 times to v1, v2, j1, j2. Anyone know how to fix this problem?***

#include<fstream>
#include<iostream>
#include<cstdlib>
#include<ctime>


using namespace std;
int const ROWS = 4;
int const COLS= 4;

void shuffleBoard (int [][COLS]); //shuffles the board
int Random(); //returns a random number from 0 to 3
void Swap( int& v1, int& v2); //swaps 2 integers


int main()
{
    
    int Board[ROWS][COLS] = {{ 1, 1, 2, 2}, //initializing the board
                      {3, 3, 4, 4},
                      {5, 5, 6, 6},
                      {7, 7, 8, 8}};
                      
shuffleBoard(Board);
    for (int i=0; i < 16; i++)
      {
              int m= 0;
              cout << Board[m][i];
              m++;                  
              }   

getchar();

return 0;

}






void shuffleBoard(int Board[][COLS])
{
     int v1, v2, j1, j2;
     const int TIMES = 100; //times to shuffle
     for (int i = 0; i < TIMES; i++)
     {
         v1 = Random();
         v2 = Random();
         j1 = Random();
         j2 = Random();
         swap(Board[v1][j1], Board[v2][j2]);
     }   
 }
 
int Random()
{
    int x;
    srand (time(NULL)); //gets a new seed based on the time
    int const M = 0, N = 3;
    x = (M+(rand()%(N-M+1)));
    return x;
}

void swap (int& v1, int& v2)
{
     int temp;
     temp = v1;
     v1 = v2;
     v2 = temp;
}

seed the generator just once at the start of the program, not every time you call random. Since the time it takes the computer to do the separate function calls is so short there isn't enough time to make a difference in the seeding so it's seeding with the same number every time.

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.