Help! Looking for java experts!!

Jmman

Diamond Member
Dec 17, 1999
5,302
0
76
Ok, I am a complete moron when it comes to Java, so this question is probably stupid. I am trying to generate 6 random numbers for some lottery calculations, and I can't dream up how to eliminate duplicate answers. What I have done so far is to create an array of 6 integers, and by using math.random and a for loop I have filled the array with random numbers from 1 to 42. I just need some advice on how to eliminate the duplicate numbers that are sometimes generated in the array. Any and all advice is appreciated.....
:eek:
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Whenever you generate a random number, check to see if it's already in the list. If it isn't, add it to the list. If it IS already in the lsit, throw it away and generate a new number.
 

mundane

Diamond Member
Jun 7, 2002
5,603
8
81
Use a collection instead of an array, first of all. The searching functionality is already in there. More specifically, a Set is designed to prevent duplicate entries, so when you add(), a false return indicates that the element you were attempting to add was already present, etc.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: diegoalcatraz
Use a collection instead of an array, first of all. The searching functionality is already in there. More specifically, a Set is designed to prevent duplicate entries, so when you add(), a false return indicates that the element you were attempting to add was already present, etc.
ding ding ding!
 

Jmman

Diamond Member
Dec 17, 1999
5,302
0
76
Yeah, but I am a beginner when it comes to Java. I have no idea how to work with sets. Thatnks Notfred, I was able to get it working with your advice. I am sure that there are simpler implementations than my ponderous code, but hey, it works....:)