Shuffling a 2D vector

How would I go about shuffling a 2D vector?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  std::vector<std::vector<char>> hidden;

	for(int i = 0; i < rows; i++){
		std::vector<char> temp2;
		for(int j = 0; j < columns / 2; j++){
			temp2.push_back(i * (columns / 2) + j + 65);
			temp2.push_back(i * (columns / 2) + j + 65);
		}
		hidden.push_back(temp2);
	}
	
	for(int i = 0; i < hidden.size(); i++) {
		for (int j = 0; j < hidden[i].size(); j++){
			shuffle(hidden[j].begin(), hidden[j].end(), std::default_random_engine(seed));
		}
	}

	shuffle(hidden.begin(), hidden.end(), std::default_random_engine(seed));


Right now, this code produces something like this:

E E F F
G G H H
C C D D
A A B B

When I'd like something like this:

C H D A
G D B F
E F A B
H E C G
Load a string with the letters. Shuffle that. Load the vector from the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>
#include <algorithm>
#include <random>
using namespace std;

int main() {
    int rows = 4, cols = 4;

    // Load a string with the letters.
    string s;
    for (int r = 0; r < rows; r++)
        for (int c = 0; c < cols; c++)
            s.push_back((r * cols + c) / 2 + 'A');

    // Shuffle the string.
    random_device rd;
    shuffle(s.begin(), s.end(), default_random_engine(rd()));

    // Load the 2d vector from the string.
    vector<vector<char>> hidden;
    for(int i = 0, k = 0; i < rows; i++) {
        vector<char> v;
        for(int j = 0; j < cols; j++)
            v.push_back(s[k++]);
        hidden.push_back(v);
    }

    // Print 2d vector.
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++)
            cout << hidden[i][j] << ' ';
        cout << '\n';
    }
}

Topic archived. No new replies allowed.