Results 1 to 3 of 3
  1. #1
    shane-01 is offline Member
    Join Date
    Apr 2013
    Rep Power

    Question A little help with my lottery numbers please

    Hi everyone

    I just want to get a bit of help with this lottery number program I'm trying to make.
    I've not been playing with java that long, only 6 days into java in 21 days by sams the sixth edition
    and I thourght I'd challenge myself so If there is any obvious errors I've made let me know.

    this is my code so far

    import java.util.Random;
    import java.util.Arrays;

    public class Lottery {

    public static void main(String[] args) {
    int[] todaysNumbers = new int[6];
    Random random = new Random();
    System.out.println("Todays numbers are ");

    //for (int i = 0; i < todaysNumbers.length; i++) {
    // todaysNumbers[i] = random.nextInt(49) + 1;

    todaysNumbers[0] = random.nextInt(49) + 1; // first number
    for (int i = 1; i < todaysNumbers.length; i++) { // loop for next 5 numbers
    todaysNumbers[i] = random.nextInt(49) + 1;
    for (int k = 1; k < i; k++) { // loop to test the latest number against the rest
    while (todaysNumbers[k] == todaysNumbers[i]) { // should generate new number if there is a duplicate ???
    todaysNumbers[k] = random.nextInt(49) + 1;
    Arrays.sort(todaysNumbers); // sorts todaysNumbers[] in to numerical order

    for (int j = 0; j < todaysNumbers.length; j++) {
    System.out.print(todaysNumbers[j] + " ");

    for some reason I'm still getting the odd duplicated number
    nut I'm not too sure why

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Rep Power

    Default Re: A little help with my lottery numbers please

    I would approach this by:

    -Creating an ArrayList of Integers(let's call it generatedNums?)
    -Go through this loop until generatedNums contains 6 Integers
    ---Generate a random number(call it r?)
    ---If generatedNums does not contain r, add r to generatedNums.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power

    Default Re: A little help with my lottery numbers please

    You can simplify the above advice by using a Set which does not allow duplicates.

    Alternatively, create and fill a List with all numbers. Shuffle the List. Read 6 numbers from the front of the List.

    But the use of some of these concepts are beyond a 6 day Java user.

Similar Threads

  1. lottery GUI program
    By jib123 in forum New To Java
    Replies: 2
    Last Post: 11-17-2012, 04:18 PM
  2. Replies: 4
    Last Post: 01-27-2012, 11:18 AM
  3. Random Lottery Numbers
    By ComicStix in forum New To Java
    Replies: 2
    Last Post: 04-29-2011, 06:18 AM
  4. lottery
    By kayln in forum Eclipse
    Replies: 0
    Last Post: 02-20-2011, 09:28 PM
  5. Lottery Application
    By notnumber6 in forum New To Java
    Replies: 3
    Last Post: 11-03-2007, 11:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts