The assignment was to write a program that generated a random sequence of six non-repeating numbers between 1 and 53 until it matched a user inputted sequence (lotto).

Here is the entire program.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cctype>
#include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

main ()
{
    char loop;
    int ticket[6];
    int lotto[6];
    int a;    
    int b;
    int c;
    int d;
    
    
    do
    {
         c = 0;
         
         for(b = 0; b <= 5; b++)
         {
               ticket[b] = 0;
         }
                  
         do
         {
             system("CLS");
             d = 0;          
             printf("Please enter your lotto numbers.\n");

             for(b = 0; b <= 5; b++)
             {
                   scanf("%i", &ticket[b]);
             }
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] == 0)
                   {
                          d = 1;
                          printf("There must be 6 non-zero, non-negative lotto numbers. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }             
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] > 53)
                   {
                          d = 1;
                          printf("Lotto numbers must be between 1 and 53. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }
             
             for(b = 0; b <= 4; b++)
             {
                   if (ticket[b] == ticket[b + 1])
                   {
                          d = 1;
                          printf("Lotto numbers cannot repeat. Try again.\n");
                          system("Pause");
                          break;
                   }
             }
          } while(d == 1);

         do
         {
             a = 0;
             for(b = 0; b <= 5; b++)
             {
                  srand ( time(NULL) );
                  lotto[b] = rand() % 53 + 1;
             }          
             
             for(b = 0; b <= 5; b++)
             {
                   if(ticket[b] != lotto[b])
                   {
                          a = 1;
                   }
             }
         
             c++;
             cout << c <<endl;
                 
         } while (a == 1);
          
         printf("Your odds are 1 to %i\n", c);
                  
         printf("Would you like to to waste your money again? Y/N.\n");
         printf(" No, seriously, you should open a savings account.\n");
         cin >> loop; 
           
         loop = tolower(loop);

    } while(loop == 'y');
}

The problem doesn't occur until the comparison between the user's sequence and the generated sequence.

do
         {
             a = 0;
             for(b = 0; b <= 5; b++)
             {
                  srand ( time(NULL) );
                  lotto[b] = rand() % 53 + 1;
             }          
             
             for(b = 0; b <= 5; b++)
             {
                   if(ticket[b] != lotto[b])
                   {
                          a = 1;
                   }
             }
         
             c++;
             cout << c <<endl;
                 
         } while (a == 1);

Is it not possible to compare arrays this way? What happens is the loop continues without stop, I realize it should take some time but 6.5 million iterations is unreasonable. Any suggestions?

The problem doesn't occur until the comparison between the user's sequence and the generated sequence.

do
         {
             a = 0;
             for(b = 0; b <= 5; b++)
             {
                  srand ( time(NULL) );
                  lotto[b] = rand() % 53 + 1;
             }          
             
             for(b = 0; b <= 5; b++)
             {
                   if(ticket[b] != lotto[b])
                   {
                          a = 1;
                   }
             }
         
             c++;
             cout << c <<endl;
                 
         } while (a == 1);

srand() should only be called once. Move it to the beginning of main() .

Is it not possible to compare arrays this way? What happens is the loop continues without stop, I realize it should take some time but 6.5 million iterations is unreasonable. Any suggestions?

You are dealing with random numbers, aren't you? There is never a guarantee using random values that a specific sequence of 6 numbers will occur. There are probabilities of the occurrence, but no guarantees.

What is the probability of a specific 6-number sequence occurring?

I moved the srand so that it is only called once for each ticket number, and I ran the program all night.

Here is the revised code.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cctype>
#include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

main ()
{
    char loop;
    int ticket[6];
    int lotto[6];
    int a;    
    int b;
    int c;
    int d;
    
    
    do
    {
         srand ( time(NULL) );
         c = 0;
         
         for(b = 0; b <= 5; b++)
         {
               ticket[b] = 0;
         }
                  
         do
         {
             system("CLS");
             d = 0;          
             printf("Please enter your lotto numbers.\n");

             for(b = 0; b <= 5; b++)
             {
                   scanf("%i", &ticket[b]);
             }
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] == 0)
                   {
                          d = 1;
                          printf("There must be 6 non-zero, non-negative lotto numbers. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }             
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] > 53)
                   {
                          d = 1;
                          printf("Lotto numbers must be between 1 and 53. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }
             
             for(b = 0; b <= 4; b++)
             {
                   if (ticket[b] == ticket[b + 1])
                   {
                          d = 1;
                          printf("Lotto numbers cannot repeat. Try again.\n");
                          system("Pause");
                          break;
                   }
             }
          } while(d == 1);

         do
         {
             a = 0;
             for(b = 0; b <= 5; b++)
             {
                  lotto[b] = rand() % 53 + 1;
             }          
             
             for(b = 0; b <= 5; b++)
             {
                   if(ticket[b] != lotto[b])
                   {
                          a = 1;
                   }
             }
         
             c++;
             cout << c <<endl;
                 
         } while (a == 1);
          
         printf("Your odds are 1 to %i\n", c);
                  
         printf("Would you like to to waste your money again? Y/N.\n");
         printf(" No, seriously, you should open a savings account.\n");
         cin >> loop;
           
         loop = tolower(loop);

    } while(loop == 'y');
}

Currently it is at 838 million iterations and counting. I think you are right, this is too improbable. I'll change it so that, instead of an exact sequence, it is only the numbers that you pick, not in any order. Somehow, I think that may be harder.

Edited 6 Years Ago by ajsavoca: n/a

For the curious, this is the revised code. It no longer matters what order the numbers are in.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cctype>
#include <iostream>
#include <windows.h>
#include <time.h>

using namespace std;

main ()
{
    char loop;
    int ticket[6];
    int lotto[6];
    int a; 
    int b; 
    int c; 
    int d; 
    int e; 
    int f;
    
    do
    {
         srand ( time(NULL) );
         c = 0;
         
         for(b = 0; b <= 5; b++)
         {
               ticket[b] = 0;
         }
                  
         do
         {
             system("CLS");
             d = 0;          
             printf("Please enter your lotto numbers.\n");

             for(b = 0; b <= 5; b++)
             {
                   scanf("%i", &ticket[b]);
             }
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] == 0)
                   {
                          d = 1;
                          printf("There must be 6 non-zero, non-negative lotto numbers. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }             
             
             for(b = 0; b <= 5; b++)
             {
                   if (ticket[b] > 53)
                   {
                          d = 1;
                          printf("Lotto numbers must be between 1 and 53. Try again.\n");
                          system("Pause");
                          break;
                          
                   }
             }
             
            for(e = 0; e <= 5; e++)
            {
                 for(b = e + 1; b <= 5; b++)
                 {
                       if (ticket[b] == ticket[e])
                       {
                              d = 1;
                              printf("Lotto numbers cannot repeat. Try again.\n");
                              system("Pause");
                              break;
                       }
                 }
                 
                 if (d == 1)
                 {
                       break;
                 }     
            }
            
          } while(d == 1);

         do
         {
             a = 0;
             for(b = 0; b <= 5; b++)
             {
                  lotto[b] = rand() % 53 + 1;
             }          
             
             for (f = 0; f <= 5; f++)
             {
                 for(b = 0; b <= 5; b++)
                 {
                       if(lotto[f] == ticket[b])
                       {
                              a++;
                       }
                 }
             }
             c++;
         } while (a != 6);
          
         printf("Your odds are 1 to %i\n", c);
                  
         printf("Would you like to to waste your money again? Y/N.\n");
         printf(" No, seriously, you should open a savings account.\n");
         cin >> loop;

         loop = tolower(loop);

    } while(loop == 'y');
}

The other program is still running, currently at 1 billion, 52 million iterations and counting.

This question has already been answered. Start a new discussion instead.