Jeremy Lin
Jeremy Lin

Reputation: 15

Random Array Repeating

So I am creating a penny game where the code will randomly choose 5 cells. 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?

Upvotes: 0

Views: 88

Answers (3)

Oneiros
Oneiros

Reputation: 4378

  1. Create a list of 25 integers (you have a 5x5 grid right?) and initialize it incrementally

    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

    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

    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.


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

Upvotes: 1

Anonymous
Anonymous

Reputation: 86379

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.

Upvotes: 1

Rahul Bhatnagar
Rahul Bhatnagar

Reputation: 106

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 very simple way to get unique pairs in a 5x5 penny array (which is what I think you are trying to achieve) is :

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);
}

Upvotes: 0

Related Questions