This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Coding" Watch "Coding" New topic
Author

Coding

Becky Miller
Greenhorn

Joined: Jan 24, 2001
Posts: 17
I am trying to write a program that requires the program to select six different numbers from integers 1 to 49 and then generate five sets of entries.
so far I have :
public class Lottery{
public static void main(String [] args)
{


int number =0;
number = 1 +(int)(49 * Math.random());
System.out.println(number);

}
}
how do you select 6 random numbers from 1 to 49?
I really don't know where to go from here. Any help would be appreciated.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Becky,
Why not just generate all 6 numbers like you have done with "number" in your code?
int num1 = 1 + (int)( 49 * Math.random() );
int num2 = 1 + (int)( 49 * Math.random() );
int num3 = 1 + (int)( 49 * Math.random() );
int num4 = 1 + (int)( 49 * Math.random() );
int num5 = 1 + (int)( 49 * Math.random() );
int num6 = 1 + (int)( 49 * Math.random() );
And you could generate the entries the same way...(if by entries you mean what I think you mean)
~Ryan

<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
Also, Don't forget to put a method in to check and see if the number has been used already. I would suggest putting all of the chosen numbers into an array, and then check the new number against the existing array elements. Something along the lines of...

should be a start.
Pat B.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

If the numbers have to be unique I would do the same as Pat suggested.


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Coding
 
Similar Threads
Minimum Value and Sum of Values in Array
Program keeps locking up, not sure why
loopy for loops
Powerball program for school, Need to to run program 8 times.
BufferedReader.read