So I am generating random coordinates on a given grid that contains x number of rows and y number of columns. Let's say I want to generate 25 random numbers on a 8 by 6 grid.(8 columns, 6 rows) I wrote piece of code like this and it is only partially working because this code does not excludes duplicates:
int intputRows =6;
int inputColumns=8;
Random randomNumGenerator = new Random();
for(int i=0;i<25;i++){
int randomRows = randomNumGenerator.nextInt(inputRows);
int randomColumns = randomNumGenerator.nextInt(inputColumns);
}
My question is, how do I avoid to generate duplicate numbers? I understand there are ways like put those in a List
structure and shuffle, but could I done it with Random
generator?