如何用 Java 中另一个整数数组的值随机填充 ant int 数组?

How to fill ant int array randomly with values from another integer array in Java?

我有一个包含 40 个整数值的整数数组,需要将它们随机放入另一个整数数组中。

我有一个随机数,它从第一个数组中选择一个随机值,但如果已经选择了那个特定的整数,它必须选择一个新的随机值,但最后一部分似乎出于某种原因出现错误。

Random rand = new Random();    

int[] availablePawnsArray = {1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12 };
// this array contains 40 integers

int[] chosenPawns = new int[40];
//this array contains the index numbers of already selected pawnsfrom the previous array

int counter = 0;
//counts how many pawns have been selected already    

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 10; j++) {
    //this refers to my board, 40 locations for my 40 pawns    

        int chosenPawn = rand.nextInt(40);
        //a random numder from 0 to 40
        
        boolean found = false;
        //a boolean to say if i have already selected this pawn before or not    

        do {
            for (int n : chosenPawns) {
                if (n == chosenPawn) {
                    found = true;
                    chosenPawn = rand.nextInt(40);
                } else {
                    found = false;
                }
            }
        } while(found == true);    

        board[i][j].rank = availablePawnsArray[chosenPawn];
        chosenPawns[counter] = chosenPawn;
        counter++;
    }
}

您可以有两个数组,第二个数组保留选定的整数,然后在第二个数组中循环检查是否有任何数字等于给定的一个 return false 或 true。

int [] selectedInts = new int[40];

boolean contains(int num) {
  for (int i = 0 ; i < selectedInts.length; i++) {
    if (i == num) return true;
  }
  return false;
}

你也可以用like

Arrays.asList().contains(yourInt);

chosenPawns 数组应该只搜索到计数器。 当几乎全部用完时,反复随机寻找一个空闲点,可能需要几千步才能找到最后的最后一个空闲点。甚至不能保证找到空位。

简单地将随机位置视为寻找第一个空闲棋子的起点,从... 36, 37, 38, 39, 0, 1, ....

int[] pawnsArray = {1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4,
    5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12 };
// this array contains 40 integers

boolean[] pawnsChosen = new boolean[40];
// this array tells wether the i'th pawn was chosen.

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 10; j++) {

        int pawnIndex = rand.nextInt(40);
        while (pawnsChosen[pawnIndex]) {
            pawnIndex = (pawnIndex + 1) % 40;
        }
        pawnsChosen[pawnIndex] = true;

        board[i][j].rank = availablePawnsArray[pawnIndex];
    }
}

您可以通过修改随机数选择来简化这一过程,就像洗一副纸牌一样。它是 Fisher-Yates 的轻微变体。我相信这应该总是在线性时间内工作。

      Random rand = new Random();
      int[] availablePawnsArray = {
            1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6,
            6, 6, 6, 7, 7, 7, 8, 8, 9, 10, 11, 12, 12, 12, 12, 12, 12
      };


      int start = 39;
      for (int i = 0; i < 4; i++) {
         for (int k = 0; k < 10; k++) {
            int chosenPawn = rand.nextInt(start + 1);
            // chose the pawn
            board[i][k].rank = availablePawnsArray[chosenPawn];
            // copy the pawn from the end of the list to the 
            // chosen pawn location.
            availablePawnsArray[chosenPawn] = availablePawnsArray[start];
            // update the random number to ignore the last slot
            // in the array (the pawn in that slot has
            // been moved to occupy the chosenPawn's location)
            start--;
         }
      }

      for (int i = 0; i < 4; i++) {
         for (int k = 0; k < 10; k++) {
            System.out.print(board[i][k].rank + " ");
         }
         System.out.println();
      }
   }

除非您需要将其作为练习来实现,否则您可以使用内置的 shuffle 方法,将可用棋子数组包装在列表中:

Collections.shuffle(Arrays.asList(availablePawnsArray));
    
for (int i = 0, k = 0; i < 4; i++) 
    for (int j = 0; j < 10; j++, k++) 
        board[i][j].rank = availablePawnsArray[k];