-
Java Arrays & Loops
Hi guys, I'm having a bit of trouble with the basics, I need to generate four unique numbers between 1 and 10 for each line. It all works fine except i cant work out how to check if the number is unique??
Code:
linesArray = new int[lines][4];
Random diceRoller = new Random();
//for each of the lines requested
for(int i = 0; i < lines; i++) {
//for each number in the line
int j = 0;
while(j < 4){
//generate a random number
int roll = diceRoller.nextInt(10) + 1;
if(linesArray[i][0] == roll || linesArray[i][1] == roll || linesArray[i][2] == roll || linesArray[i][3] == roll) {
return;
} else {
linesArray[i][j] = roll;
j++;
}
}
}
thanks in advance for any help you can give!Adam
-
Re: Java Arrays & Loops
Store the ten numbers [1 ... 10] in a Collection, call Collections.shuffle( ... ) and get the first four numbers of that Collections.
kind regards,
Jos
-
Re: Java Arrays & Loops
Got it sorted, thanks so much for your help.