I'm creating a small program that extracts 3 random numbers, the only condition is that all three numbers must be different from each other, for example: 3,9,3 is not acceptable.
This i my code, i tried it several times and occasionally appear numbers equal to each other.
What is wrong in my code ?
public class Premi {
public static void main(String[] args) {
int num = (int) (Math.random() *10) + 1;
int num2 = (int) (Math.random() *10) + 1;
int num3 = (int) (Math.random() *10) + 1;
boolean first = true;
boolean second = true;
boolean third = true;
while(first) {
if (num!=num2) {
first=false;
} else if (num==num2) {
num = (int) (Math.random() *10) + 1;
}
}
while(second) {
if (num!=num3) {
second=false;
} else if (num==num3) {
num = (int) (Math.random() *10) + 1;
}
}
while(third) {
if (num2!=num3) {
third=false;
} else if (num2==num3) {
num2 = (int) (Math.random() *10) + 1;
}
}
System.out.println(num + "," + num2 + "," + num3);
}
}
Thank you.