lottery problem number = (int) ( range * Math.random() ) + 1;

I am new at this so any source code will greatly appreciated. I need to generate a program that generate five sets of six numbers for the result. For instance, a lottery requires that you select six different numbers from the integers 1 to 49.

Output should be created using System.out.print (...) not graphical components or applets.

I believe I need to use random() static method of class Math. It returns a double so I will need to cast it as an integer. If I set the range to 49, I can generate a random number between 1 and 49 through:

number = (int) ( range * Math.random() ) + 1;

Note that I need 5 sets of numbers and in each set I 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.

Here is an example of a valid set of numbers:

5, 41, 3, 9, 22, 30

Here is an example of an invalid set of numbers: 15, 8, 19, 33, 8, 21

It is invalid because the number 8 appears twice.

[1106 byte] By [dominoguru2] at [2007-9-27 22:27:11]
# 1

well heck, if you cant code,

maybe you could post a little pseudo-code?

I mean maybe just a little something...

or maybe post just a little code?

public class Lotto{

public static void main(string args[]){

//code to generate one number

//code to display one number

}//end of main

}//end of class

start small...

MSwirski at 2007-7-7 12:58:39 > top of Java-index,Archived Forums,Java Programming...
# 2
Look at this thread: http://forum.java.sun.com/thread.jsp?forum=31&thread=312846
danbas at 2007-7-7 12:58:39 > top of Java-index,Archived Forums,Java Programming...
# 3

import java.util.*;

public class Lotto{

static final int TOTAL=6, BASE=49, SEQUENCES=5;

public static void main(String args[]){

if(TOTAL>=BASE){

System.out.println("incorrect task!");

System.exit(0);

}

int quantity;

Integer number;

Set s = new HashSet();

for (int i=0;i<SEQUENCES;i++){

quantity=0;

do{

number = new Integer((int)(BASE*Math.random()+1));

if (!s.contains(number)){

s.add(number);

quantity++;

}

}

while(!(quantity==TOTAL));

//you can sort, transform to int or save here your hashSet

System.out.println(s);

s.clear();

}

}

}

Congratulation! Your homework is done!>

Alex_Pershyn at 2007-7-7 12:58:39 > top of Java-index,Archived Forums,Java Programming...