简体   繁体   English

随机数组重复

[英]Random Array Repeating

So I am creating a penny game where the code will randomly choose 5 cells. 因此,我正在创建一个便士游戏,其中的代码将随机选择5个单元格。 I set it like so: 我将其设置为:

    int a = gen.nextInt(5);
    int b = gen.nextInt(5);
    int c = gen.nextInt(5);
    int d = gen.nextInt(5);
    int e = gen.nextInt(5);
    int f = gen.nextInt(5);
    int g = gen.nextInt(5);
    int h = gen.nextInt(5);
    int i = gen.nextInt(5);
    int j = gen.nextInt(5);
    int penny1 = Parray[a][b];
    int penny2 = Parray[c][d];
    int penny3 = Parray[e][f];
    int penny4 = Parray[g][h];
    int penny5 = Parray[i][j];

The problem is that sometimes the random cells are repeated. 问题在于有时随机单元会重复。

How can I make it so a random array cell cannot be repeated or chosen again? 我如何才能使随机数组单元不能重复或再次选择?

Depending on your exact requirements here is an option: 根据您的确切要求,这里是一个选项:

    List<Integer> pennies = new ArrayList<>(NUMBER_OF_PENNIES);
    for (int p = 0; p < NUMBER_OF_PENNIES; p++) {
        Integer penny;
        do {
            int a = gen.nextInt(5);
            int b = gen.nextInt(5);
            penny = pArray[a][b];
        } while (pennies.contains(penny));
        pennies.add(penny);
    }

This will make sure that the list of pennies does not have any values repeated. 这将确保便士列表中没有重复任何值。 If instead you don't want any cell indices repeated, it's getting a bit more complicated, but a similar technique can be used. 相反,如果您不希望重复任何单元格索引,则会变得有些复杂,但是可以使用类似的技术。

I took the freedom of renaming your 2D array to pArray to follow Java naming conventions. 我可以自由地将2D数组重命名为pArray以遵循Java命名约定。

  1. Create a list of 25 integers (you have a 5x5 grid right?) and initialize it incrementally 创建一个由25个整数组成的列表(您有5x5网格吗?)并逐步对其进行初始化。

     List<Integer> indexes = new ArrayList<>(); for (int i = 0; i < 25; i++) indexes.add(i); 
  2. Shuffle it 随机播放

     Collections.shuffle(indexes); 
  3. (Optional step, not necessary) Shrink the list since you need just first 5 indexes (可选步骤,不是必需的)缩小列表,因为您只需要前5个索引

     indexes.subList(5, indexes.size()-1).clear(); //removing from index 5 to 24 
  4. Convert each of these indexes to row-column coordinates (for example: index 8 corresponds to the element in 2nd row, 3rd column) and store the corresponding penny 将这些索引中的每一个转换为行列坐标(例如:索引8对应于第二行第三列中的元素),并存储相应的便士

     List<Integer> pickedPennies = new ArrayList<>(); for (int i = 0; i < 5; i++) { int row = indexes.get(i) / 5; int col = indexes.get(i) % 5; pickedPennies.add(Parray[row][col]); } 

I didn't test it but I guess the idea is pretty easy. 我没有测试,但是我想这个主意很简单。 With this approach you can avoid to implement an ugly while loop to check if you already picked a penny. 使用这种方法,您可以避免执行丑陋的while循环来检查您是否已选择一分钱。


Alternative : 替代方案

Store your pennies in a list 将您的便士存储在列表中

List<Integer> pickedPennies = new ArrayList<>();
for (int i = 0; i < 5; i++)
   for (int j = 0; j < 5; j++)
      pickedPennies.add(Parray[i][j]);

And shuffle it 并洗牌

Collections.shuffle(pickedPennies);

First 5 elements of the list are your random pennies 列表的前5个元素是您的便士

All you need to do is ensure that the combinations (a,b), (c,d), etc. are unique so that your pennies are also unique. 您要做的就是确保组合(a,b),(c,d)等是唯一的,以便您的便士也唯一。 A very simple way to get unique pairs in a 5x5 penny array (which is what I think you are trying to achieve) is : 在5x5美分数组中获得唯一对的一种非常简单的方法(这是我认为您正在尝试实现的):

public static boolean checkIfComboExists(ArrayList<int[]> map,int[] combo){
    for(int i = 0; i < map.size(); i++){
        int[] elem = map.get(i);
        if(elem[0] == combo[0] && elem[1] == combo[1]){
            return true;
        }
    }
    return false;
}

public static void main(String[] args){

    int[][] Parray = {{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}};
    Random gen = new Random();

    ArrayList<int[]> map = new ArrayList<int[]>();
    while (map.size() < 5){
        int x = gen.nextInt(5);
        int y = gen.nextInt(5);
        int[] combo = {x,y};
        if(!checkIfComboExists(map,combo)){
            map.add(combo);
        }
    }

    int newpenny1 = Parray[map.get(0)[0]][map.get(0)[1]];
    int newpenny2 = Parray[map.get(1)[0]][map.get(1)[1]];
    int newpenny3 = Parray[map.get(2)[0]][map.get(2)[1]];
    int newpenny4 = Parray[map.get(3)[0]][map.get(3)[1]];
    int newpenny5 = Parray[map.get(4)[0]][map.get(4)[1]];

    System.out.println(newpenny1);
    System.out.println(newpenny2);
    System.out.println(newpenny3);
    System.out.println(newpenny4);
    System.out.println(newpenny5);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM