I'm kinda new to Java and I've been trying to implement a sort of Lottery game, where the user inputs 7 numbers (5 numbers and 2 "stars") and if they match with the random generated numbers, the users wins the lottery.
I'm storing the random numbers in two different ArrayLists (one for the numbers, and another for the stars), but I need to get rid of duplicates and restrict the range of the numbers.
Numbers can go from 1 to 50. Stars can go from 1 to 10. And there can't be duplicated numbers in both Arrays. What's the easiest way to accomplish this.
ArrayList <Integer> randomNumbers = new ArrayList<Integer>();
ArrayList <Integer> randomStars = new ArrayList<Integer>();
//5 random numbers into ArrayList randomNumbers;
int saveRandom;
Random rand = new Random();
for (int i=0; i <5; i++) {
saveRandom = rand.nextInt(50);
randomNumbers.add(saveRandom);
}
Collections.sort(randomNumbers);
System.out.println("Lottery Numbers:" + randomNumbers);
//2 random numbers into ArrayList stars
int saveStar;
Random randStar = new Random();
for(int a=0; a < 2; a++) {
saveStar = randStar.nextInt(10);
randomStars.add(saveStar);
}
Collections.sort(randomStars);
System.out.println("Lottery stars:" + randomStars);
Aucun commentaire:
Enregistrer un commentaire