• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Coding

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the numbers have to be unique I would do the same as Pat suggested.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic