Need some help or advice in C++ program

This is a discussion on Need some help or advice in C++ program within the C++ Programming forums, part of the General Programming Boards category; I need just a little help with my program,program rans and it will display 10 different lotto numbers. However i ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    4

    Post Need some help or advice in C++ program

    I need just a little help with my program,program rans and it will display 10 different lotto numbers. However i raninto a problem, everytime i run it i get numbers that are the same in the draw. Can anyone give me some advice or help on how i can prevent that from happening. Can i do FOR statement to reset all to 0 or set array to 0? how would i go doing that? I included the program below. Thanks for the help

    Code:
    //sp
    //11/30/04
    
    #include <iostream>
    
    using namespace std;
    
    #include <iomanip>
    
    
    using std::setw;
    
    
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
           const int size = 6;
           int a[size] = {0,0,0,0,0,0};
           srand(time(0));
    
    
           for(int w=1; w<11;w++){
               cout <<"For game #"<< setw(3) << w << "=";
    
               for(int i=0;i<size;i++){
                   a[i] = rand()%59 + 1;
                   cout << setw(5) << a[i] <<"  ";
                        }
               cout <<endl;
                    }
                cout <<endl;
                return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I'm afraid I don't understand your problem. The program works fine and gives seemingly random numbers. Can you give an example of what you're getting as well as an example of what you want to get?
    My best code is written with the delete key.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Are you referring to the program giving you 10 random numbers at first, but then giving you those same numbers each time you run the program?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >then giving you those same numbers each time you run the program?
    If this is the case then srand isn't doing it's job on the OP's implementation. Which is entirely possible since using time directly to seed srand is non-portable. However, clarification would be a good idea before we start throwing out ideas.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    the problem is that i get the following output:

    For game # 1= 42 36 37 51 57 57 <-------------- There are two 57's, there cant be 2 same numbers in a draw for lotto.
    For game # 2= 21 35 20 20 44 14
    For game # 3= 5 8 2 31 59 12
    For game # 4= 45 9 33 10 23 38
    For game # 5= 25 12 30 2 50 12
    For game # 6= 54 43 8 34 54 21
    For game # 7= 10 23 43 18 48 52
    For game # 8= 30 47 37 23 12 5
    For game # 9= 42 29 58 47 56 47
    For game # 10= 5 28 13 43 47 38

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    You'll need a function to check for duplicate, then. While it's true, keep re-generate a new random number.
    Just go through previously generated random number. Not the fastest algorithm, but fast enough for small count of random number.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    &operator overload neolyn's Avatar
    Join Date
    Nov 2004
    Location
    Morgantown, WV
    Posts
    16
    Try this:

    Code:
    //sp
    //11/30/04
    
    #include <iostream>
    
    using namespace std;
    
    #include <iomanip>
    
    
    using std::setw;
    
    
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
           const int size = 6;
           int a[size] = {0,0,0,0,0,0};
           int tmp, j;
           j = 0;
           srand(time(0));
    
    
           for(int w=1; w<11;w++){
               cout <<"For game #"<< setw(3) << w << "=";
    
               for(int i=0;i<size;i++){
    	       tmp = rand()%59 +1;
                   while(j < size) {
                     if(a[j] == tmp) {        // if number has already been taken
    		    tmp = rand()%59 + 1;  // generate new rand
    	            j=0;
    	         }
    	         else {
    		    j++;
    		 }
    	       }
                   a[i] = tmp;
                   cout << setw(5) << a[i] <<"  ";
                        }
               cout <<endl;
                    }
                cout <<endl;
                return 0;
    }
    Note the inner while loop inside your nested for loop. This loop will check the current contents of your array for a duplicate number - if the number has already been called, the loop will reset and generate a new rand to try again.

    I didn't compile this, but it should work just fine. If anything, you should get the general idea. Hope this helps.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >there cant be 2 same numbers in a draw for lotto.
    Make sure there aren't any duplicates then. You could do it with loops and such, but that's not very efficient. A better way is to take a random sample of a list of valid draws:
    Code:
    #include <algorithm>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <iterator>
    
    using namespace std;
    
    void random_draw(int a[], int size)
    {
      int list[59];
    
      for (int i = 0; i < 59; i++)
        list[i] = i + 1;
    
      for (int i = 0; i < size; i++) {
        int r = rand() % (59 - i) + 1;
        a[i] = list[i + r];
      }
    }
    
    int main()
    {
      int a[5];
    
      for (int i = 1; i <= 5; i++) {
        random_draw(a, 5);
        cout<<"Game #"<< i <<": ";
        copy(a, a + 5, ostream_iterator<int>(cout, " "));
        cout<<endl;
      }
    }
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    thanks alot for all the help, the program still gives me double number, but i do get the general idea of it and thats all i need i will play around with it and try to get it to work.


    thank you!!!

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >the program still gives me double number
    Which solution did you use?
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    4
    thanks to all of your help specially neolyn i used his but i had to add 1 line of code.

    at the end of code i added j=0; since it reseted it in loop but not out of it.

    so the program looks like this now

    Code:
    //sp
    //11/30/04
    
    #include <iostream>
    
    using namespace std;
    
    #include <iomanip>
    
    
    using std::setw;
    
    
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
           const int size = 6;
           int a[size] = {0,0,0,0,0,0};
           int tmp, j;
           j = 0;
           srand(time(0));
    
    
           for(int w=1; w<11;w++){
               cout <<"For game #"<< setw(3) << w << "=";
    
               for(int i=0;i<size;i++){
    	       tmp = rand()%59 +1;
                   while(j < size) {
                     if(a[j] == tmp) {        // if number has already been taken
    		    tmp = rand()%59 + 1;  // generate new rand
    	            j=0;
    	         }
    	         else {
    		    j++;
    		 }
    	       }
                   a[i] = tmp;
                    j = 0;          // added this line to the program 
                   cout << setw(5) << a[i] <<"  ";
                        }
               cout <<endl;
                    }
                cout <<endl;
                return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  3. Menu program advice
    By Ne0 in forum C Programming
    Replies: 4
    Last Post: 12-04-2004, 04:27 AM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 08:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21