This is my first program apart from the infamous "HelloWorld" one. The purpose of this program is to simulate a lottery number generator. The program is printing numbers which is great, but it isn't behaving like how it should. It's supposed to print 100 number combinations and it only prints 1 to approximately 8-10. It looks like it's stuck in a loop somewhere? Also, I'm not sure if my syntax is correct in relation to the "breaks" on the loops and so forth. Lastly, when I run it in the CMD prompt the cursor doesn't go back to the C:\ root after the program has executed. Again, this is why I think it's stuck in a loop. Any comments will be much appreciated.

/*
  * 02-16-2013
  * This program is for selecting 100 random number combinations for the Powerball lottery. Each number combination contains six separate numbers.
  * If it works correctly it should automatically display 100 number combinations.
  */
import java.util.Random;

public class Lotto2 {
    public Lotto2() {

// The range for the first five numbers is 1-59.    
    int firstNumber;
    int secondNumber;
    int thirdNumber;
    int fourthNumber;
    int fifthNumber;
// The range for the sixth number is 1-35.
    int sixthNumber;

    Random rand = new Random();
    firstNumber = rand.nextInt(59) + 1; //How I got the range: 59-1+1=59 which translates to (59) + 1.
    secondNumber = rand.nextInt(59) + 1; 
    thirdNumber = rand.nextInt(59) + 1; 
    fourthNumber = rand.nextInt(59) + 1; 
    fifthNumber = rand.nextInt(59) + 1; 
    sixthNumber = rand.nextInt(35) + 1; 

    int a = firstNumber;
    int b = secondNumber;
    int c = thirdNumber;
    int d = fourthNumber;
    int e = fifthNumber;

// Testing firstNumber so that "a != b,c,d,e" Note: At first I tried  if(a != b,c,d,e){ ... }, but it wouldn't work? 
 while (a == b || a == c || a == d || a == e) { // && and, || or

     for (int i = a; i == b; i++){
         i = rand.nextInt(59) + 1;
         if (a != b){ break;
         } }

            for (int i2 = a; i2 == c; i2++){
            i2 = rand.nextInt(59) + 1;
             if (a != c){ break;
             } }

                for (int i3 = a; i3 == d; i3++){
                i3 = rand.nextInt(59) + 1;
                 if (a != d){ break;
                 } }

                    for (int i4 = a; i4 == e; i4++){
                    i4 = rand.nextInt(59) + 1;
                     if (a != e){ break;
                     } }

            }

// Testing secondNumber so that "b != a,c,d,e"
 while (b == a || b == c || b == d || b == e) {

         for (int i = b; i == a; i++){
         i = rand.nextInt(59) + 1;
         if (b != a){ break;
         } }

         for (int i2 = b; i2 == c; i2++){
                i2 = rand.nextInt(59) + 1;
                 if (b != c){ break;
                 } }

                    for (int i3 = b; i3 == d; i3++){
                    i3 = rand.nextInt(59) + 1;
                     if (b != d){ break;
                     } }

                        for (int i4 = b; i4 == e; i4++){
                        i4 = rand.nextInt(59) + 1;
                         if (b != e){ break;
                         } }


        }

//Testing thirdNumber so that "c != a,b,d,e"
 while (c == a || c == b || c == d || c == e) {

         for (int i = c; i == a; i++){
         i = rand.nextInt(59) + 1;
         if (c != a){ break;
         } }

                for (int i2 = c; i2 == b; i2++){
                i2 = rand.nextInt(59) + 1;
                if (c != b){ break;
                 } }

                    for (int i3 = c; i3 == d; i3++){
                    i3 = rand.nextInt(59) + 1;
                    if (c != d){ break;
                     } }

                        for (int i4 = c; i4 == e; i4++){
                        i4 = rand.nextInt(59) + 1;
                        if (c != e){ break;
                         } }

            }
//Testing fourthNumber so that "d != a,b,c,e"
 while (d == a || d == b || d == c || d == e) {

         for (int i = d; i == a; i++){
         i = rand.nextInt(59) + 1;
         if (d != a){ break;
         } }

                for (int i2 = d; i2 == b; i2++){
                i2 = rand.nextInt(59) + 1;
                 if (d != b){ break;
                 } }
                    for (int i3 = d; i3 == c; i3++){
                    i3 = rand.nextInt(59) + 1;
                     if (d != c){ break;
                     } }
                        for (int i4 = d; i4 == e; i4++){
                        i4 = rand.nextInt(59) + 1;
                        if (d != e){ break;
                         } }
        } 
//Testing fifthNumber so that "e != a,b,c,d"
 while (e == a || e == b || e == c || e == d) {

         for (int i = e; i == a; i++){
         i = rand.nextInt(59) + 1;
         if (e != a){ break;
         } }

                for (int i2 = e; i2 == b; i2++){
                i2 = rand.nextInt(59) + 1;
                 if (e != b){ break;
                 } }

                    for (int i3 = e; i3 == c; i3++){
                    i3 = rand.nextInt(59) + 1;
                     if (e != c){ break;
                     } }

                        for (int i4 = e; i4 == d; i4++){
                        i4 = rand.nextInt(59) + 1;
                         if (e != d){ break;
                         } }


        }

 System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + "/" + " " + sixthNumber);

    }

public static void main (String[] args){
    for (int i = 0; i < 100; i++){ // This should make it print 100 lottery numbers that contain six separate numbers in each.
        Lotto2 lot = new Lotto2();  
        }
    }
}

Recommended Answers

All 2 Replies

I can't follow all your logic, but it seems your while loops are based on values of a..e, but inside the loop you never change any of those variables, so once you enter the loop you will never leave it.
What may be easier is to generate set of nubers and test for duplicates. As long as there is any duplicate just keep generating a new set of numbers., ie

do {
   generate numbers
   test for duplicates
} while there any any duplicates

Thanks for the response. I played around with your input, but had a hard time getting it to work. Ultimately, I used some if statements along with some revised logic. I'm pretty sure my way is not the most efficient way to do it. So I definitely need more practice.Anyways, it works now. I also tried sorting the numbers from least to greatest with a array that uses bubble sorting, but failed miserably. Anyways, here's the new functional code:

/*
  * 02-16-2013
  * This program is for selecting 100 random number combinations for the Powerball lottery. Each number combination contains six separate numbers.
  * If it works correctly it should automatically display 100 number combinations.
  */
import java.util.Random;

public class Lotto6 {
    public Lotto6() {

// The range for the first five numbers is 1-59.    
    int firstNumber;
    int secondNumber;
    int thirdNumber;
    int fourthNumber;
    int fifthNumber;
// The range for the sixth number is 1-35.
    int sixthNumber;

    Random rand = new Random();
    firstNumber = rand.nextInt(59) + 1; 
    secondNumber = rand.nextInt(59) + 1; 
    thirdNumber = rand.nextInt(59) + 1; 
    fourthNumber = rand.nextInt(59) + 1; 
    fifthNumber = rand.nextInt(59) + 1; 
    sixthNumber = rand.nextInt(35) + 1; 

    int a = firstNumber;
    int b = secondNumber;
    int c = thirdNumber;
    int d = fourthNumber;
    int e = fifthNumber;



    if (a == b || a == c || a == d || a == e || b == a || b == c || b == d || b == e || 
    c == a || c == b || c == d || c == e ||
    d == a || d == b || d == c || d == e ||
    e == a || e == b || e == c || e == d) {
        for (long i = 0; i < 1000000000000L; i++){

            a = rand.nextInt(59) + 1;       
            b = rand.nextInt(59) + 1; 
            c = rand.nextInt(59) + 1; 
            d = rand.nextInt(59) + 1; 
            e = rand.nextInt(59) + 1; 

                 if (a != b && a != c && a != d && a != e && b != a && b != c && b != d && b != e && 
                 c != a && c != b && c != d && c != e &&
                 d != a && d != b && d != c && d != e &&
                 e != a && e != b && e != c && e != d){ 
                     break;
                 }}}


 System.out.println(a + " " + b + " " + c + " " + d + " " + e + " " + "/" + " " + sixthNumber); 

    }

public static void main (String[] args){
    for (int i = 0; i < 100; i++){ // This should make it print 100 lottery numbers that contain six separate numbers in each.
        Lotto6 lot = new Lotto6();  
        if (i == 99){ 
            break;
        }
        }
}}
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.