I want to generate 6 different random numbers by using Math.random and store all of them into an array. How can I make sure that they are different? I only have this so far and this has a bug. I only need numbers between 1 and 49. ( 1 + (int) (Math.random() * 49) ).
public static void main (String []args) {
int []randomNumArray = new int [6];
randomNumArray[0] = randomNumber();
System.out.println(randomNumArray[0]);
for (int i = 1; i < 6; i++) {
for (int j = 0; j < i; j++) {
randomNumArray[i] = randomNumber();
do {
if (randomNumArray[i] == randomNumArray[j]) {
randomNumArray[i] = randomNumber();
}
} while(randomNumArray[i] == randomNumArray[j]);
}
System.out.println(randomNumArray[i]);
}
}
//This method is for generating random numbers
public static int randomNumber (){
return ( 1 + (int) (Math.random() * 49) );
}