I have a query I am currently working on a program with a lottery theme I will post the parameters as well as my code thus far. A lottery requires that you select six different numbers from the integers 1 to 49. Write a Java program that will do this for you and generate five sets of six numbers as a result.
Details:
For generating random numbers you can use the random() static method of class Math. It returns a double so you will need to cast it as an integer. If you set the range to 49, you can generate a random number between 1 and 49 through:
number = (int) ( range * Math.random() ) + 1;
Note that you need 5 sets of numbers and in each set you have should have six different numbers. There should not be duplicate numbers within each set. Of course the same number can occur in multiple sets, but within the same set of 6 numbers it should only occur once, if at all.

Ok The code I have thus far is

import java.util.Scanner;
import java.util.Random;

public class Lottery 
{
    public static void main(String[] args)
    {
            int[] numbers = new int[6];
                number = (int) (range 49 Math.random() ) + 1;

I am wondering if I am coming close to being right or am I completly wrong?? I ask you the gurus of Java please help me???

Your approach is correct.

You can try with Math.random()*49..no need to add 1.

Use set instead of using array and add to set unless the size of the set reaches 6.

You can use:

Set numbers=new HashSet();
numbers.add(number);

it will help you to eliminate duplicates.

Feel free to contact me at [I]<< email snipped >>[/I]

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.