如何创建具有特定数量的 true 的随机布尔二维数组?

How to create a random boolean 2D array with a specific number of true?

我正在尝试创建一个扫雷游戏,但对于随机化部分,我遇到了一些麻烦。

对于一个 10 x 10 网格的扫雷器,我希望随机获得 10 个或特定数量的 bombs/mines,但不确定该怎么做。

我有一个 2D 地雷数组,如果它包含炸弹,则存储 TRUE。

我知道在一维数组中我可以将元素总数分成 n 个相等的部分,然后每个部分有一个 TRUE 布尔元素。

啊,但它不必平均分配。 我也不太确定列表,使用列表是解决这个问题的好方法吗?

final int GRID_WIDTH = 10;
final int GRID_HEIGHT = 10;
final int BOMB_NUMBER = 10;
final boolean[][] minesArray = new boolean[GRID_WIDTH][GRID_HEIGHT];
for (int i = 0; i < BOMB_NUMBER; i++) {
     //Get random position for the next bomb
     Random rand = new Random();
     int row = rand.nextInt(GRID_WIDTH);
     int col = rand.nextInt(GRID_HEIGHT);
     while(minesArray[row][col]) { //if this position is a bomb
          //we get new position
          row = rand.nextInt(GRID_WIDTH);
          col = rand.nextInt(GRID_HEIGHT);
     }
     minesArray[row][col] = true; //make new position is a bomb
}