• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Help! Looking for java experts!!

Jmman

Diamond Member
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.....
😱
 
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.
 
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.
 
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!
 
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....🙂
 
Back
Top