I know this question has been answered out there, but those solutions don't fit in with the way I'm going about, so I'm enquiring to see if there is a simpler solution.
I'm using the set interface and I need there to be 6 random numbers and you can't have duplicates in the set interface.
This is what I've currently got, the way I I'm doing it is not ideal and often causes crashes.
public void drawLotto(){ //The validation I have here I know isn't the most effective way and is-
Random r = new Random();//resource comsuning but this was the only way I could think of doing it.
int draw[] = new int[6];
int min = 1;
for(int i = 0; i < draw.length; i++){
draw[i] = r.nextInt(lotteryMax-min) + min;
lotteryDraw.add(draw[i]);
}
int size = lotteryDraw.size();
if(size != 6){
drawLotto();
}
for(int i = 0; i < draw.length; i++){
System.out.println(draw[i] + " ,");
}
System.out.println();
}
``
Thank you, any help is appreciated.
r.nextInt(lotteryMax-min) + minYour pool of numbers is likely very small. Just add all the possible numbers to an array/list, then SHUFFLE it and take the first six values. This will guarantee no duplicates...