This question already has an answer here:
- Creating random numbers with no duplicates 15 answers
I want to create a list of 10 unique integer pairs and I'm struggling to come up with a way to do it. For example,
1,2
8,3
.
.
.
7,0
I have a 10x10 2d array and I'm using Random to create random integers like so.
Random randGen = new Random();
int X = randGen.nextInt(10);
int Y = randGen.nextInt(10);
And then marking the 10x10 locations using the random pairs as my x and y coordinates.
The problem is I don't always come up with unique pairs of X and Y. Below you can see that 3,9 shows up twice in count 6 and count 8.
X and Y: 8,8 count = 0
X and Y: 7,9 count = 1
X and Y: 8,0 count = 2
X and Y: 8,1 count = 3
X and Y: 8,7 count = 4
X and Y: 2,0 count = 5
X and Y: 3,9 count = 6
X and Y: 0,9 count = 7
X and Y: 3,9 count = 8
X and Y: 0,1 count = 9
0 1 2 3 4 5 6 7 8 9
0 [ ][X][ ][ ][ ][ ][ ][ ][ ][X]
1 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
2 [X][ ][ ][ ][ ][ ][ ][ ][ ][ ]
3 [ ][ ][ ][ ][ ][ ][ ][ ][ ][X]
4 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
5 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
6 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
7 [ ][ ][ ][ ][ ][ ][ ][ ][ ][X]
8 [X][X][ ][ ][ ][ ][ ][X][X][ ]
9 [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
How should I go about ensuring unique int pairs. Thank you.
Aucun commentaire:
Enregistrer un commentaire