I am working on this lotto project which generates 6 random numbers in which are put into an array. How do i put it in an array where the numbers don't repeat?
peedi
0
Light Poster
Recommended Answers
Jump to PostWhenever you generate a new random number before you put it at the array check if the array already has it. If it does calculate it again, else put it and generate the next number.
Jump to PostwinningNum is an array. You need to put the first number into winningNum[0], the next into winningNum[1], etc
Jump to PostLike this:
winningNum[0] = num1; winningNum[1] = num2; winningNum[2] = num3; winningNum[3] = num4; winningNum[4] = num5; winningNum[5] = num6;
Jump to Postsome reason i get a weird display output. its like not real numbers.
You can't print an array like that. Try looping through each element:
for (int i = 0; i < 6; i++) { System.out.print (winningNum[i] + " "); }
Jump to Postone other problem, how do i make so that no numbers repeat?
Already been answered:
Whenever you generate a new random number before you put it at the array check if the array already has it. If it does calculate it again, else put it and generate the next number.
All 18 Replies
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
peedi
0
Light Poster
peedi
0
Light Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
peedi
0
Light Poster
VernonDozier
2,218
Posting Expert
Featured Poster
peedi
0
Light Poster
VernonDozier
2,218
Posting Expert
Featured Poster
peedi
0
Light Poster
javaAddict
900
Nearly a Senior Poster
Team Colleague
Featured Poster
peedi
0
Light Poster
BestJewSinceJC
700
Posting Maven
peedi
0
Light Poster
Zibo
2
Light Poster
BestJewSinceJC
700
Posting Maven
peedi
0
Light Poster
peedi
0
Light Poster
BestJewSinceJC
700
Posting Maven
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.