0

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?

3
  • 2
    How about not repeating lines of code? You need to be using a loop. I would also put cell coordinates into some sort of array (2 columns, 5 rows. each row is a pair). After each loop where you get random values for each pair, check the ones that exist already in the array and re-roll if it already exists. Commented Mar 7, 2017 at 16:19
  • Well, when putting 5 different values into 10 variables, you cannot avoid repeating some. Is it okay for b and c to repeat a as long as (c, d) doesn’t repeat (a, b), etc.? Commented Mar 7, 2017 at 16:37
  • Also, is it possible that penny2 repeats penny1 even if it is taken from a different cell of Parray? If so, is this allowed? In other words, may the 2D array contain duplicartes? Commented Mar 7, 2017 at 16:40

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1
  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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.