随机std :: array

geo*_*ano 0 c++ arrays random c++11

我如何随机制作4x4 array<array<int,4>,4>?这2D阵列的每个元素应该是唯一的号码从区间从015(含).

例:

6   7  5  4
10 11  12 15
1   3  2  8
9  14  0  13
Run Code Online (Sandbox Code Playgroud)

Lih*_*ihO 5

以下是您可以遵循的一些步骤:

  1. 定义你的数组: std::array<std::array<int, 4>, 4> myArray;
  2. 准备一个大小的临时std :: vector16,它包含来自0to的值15.
  3. 随机重新排序此向量的元素,我建议你看看std :: random_shuffle
  4. 使用此向量初始化您的数组