Filling an array at random locations with 2 of a number

I need to fill a 2 dimensional array with 2 of the same random number at 2 random locations.
I attempted to create an array fill it with 0 then check if I can fill it with a number. This seems like it would take excessive looping to fill the array.
I haven't been able to find anything that does this and ensures that there are 2 of each number inserted randomly.
Thanks for any help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

void Memory::fill()
{
    int row = random_loc();
    int column = random_loc();
    int number = random_num();

    cout << row << column << number;
 // loop until random 0 and fill it
   while(tempgrid[row][column]!=0) {
        int row = random_loc();
        int column = random_loc();
        cout << row << column << endl;
    }
   
    tempgrid[row][column] = number;
  // loop until random 0 then fill the second 
    while(tempgrid[row][column]!=0) {
        int row = random_loc();
        int column = random_loc();
    }
    tempgrid[row][column] = number;

Last edited on
I don't quite get what your problem is but your code doesn't seem to be doing anything useful ?

My interpretation is fill a number ( a randomly generated one ) in 2 seperate random location just once ?.

what does random_loc() do ?

Here is how you fill the whole array with 0
memset( tempgrid, 0, sizeof(tempgrid));

or

1
2
3
for( int i = 0; i < MAX_ROW; ++ i )
   for( int k = 0; k < MAX_COL; ++ k )
      tempgrid[i][k] = 0;


then

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int n = random_num();
int r1 = rand() % MAX_ROW;
int c1 = rand() % MAX_COL; // what I assume your random_loc() do 
int r2,c2;

// ensure the 2 location is different, although unlikely and I think it wouldn't even happened..
do {
     r2 = rand() % MAX_ROW;
     c2 = rand() % MAX_COL;
} while( r1 == r2 && c1 == c2 ); 

tempgrid[r1][c1] = n;
tempgrid[r2][c2] = n;

Last edited on
rmxhaha, the problem with your code is that it can overwrite a cell that's already been set, thus violating the requirement that each random number appears twice.

m2bandit, you have the right idea for placing two numbers, but your code can be simplified slightly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Find an empty random cell.
void getRandomEmptyCell( int &row, int &col)
{
    do {
        row = random_loc();
        col = random_loc();
    } while (tempgrid[row][col]);
}

Memory::fill()
{
    int num = random_num();
    int row, col;
    getRandomEmptyCell(row,col);
    tempgrid[row][col] = num;
    getRandomEmptyCell(row,col);
    tempgrid[row][col] = num;
}


Now if your goal is to fill the entire array this way then the technique is different. The idea is to create an array representing all indexes in the matrix. Then do a random shuffle of the array. Then pick a random number and assign it to the first two items of the shuffled array. Then pick another random number and assign it to the next two items, etc.
I didn't even think about shuffling the array. Thanks a lot, that should get rid of all of the unnecessary looping.
Topic archived. No new replies allowed.