Array question

This is a discussion on Array question within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I was just messing around in C++ and decided to make a random number generator to select lottery ...

  1. #1
    Registered User MrMatt027's Avatar
    Join Date
    Nov 2010
    Location
    USA, Florida
    Posts
    21

    Array question

    Hello everyone,
    I was just messing around in C++ and decided to make a random number generator to select lottery numbers. It looks like this:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <time.h>
    using namespace std;
    
    int main(){
    
        int const low = 1;
        int const high = 53;
        int A=0, B=0, C=0, D=0, E=0 , F=0;
        time_t seconds;
        time(&seconds);
        srand((unsigned int) seconds);
    
        A = rand() % (high - low + 1) + low;
        B = rand() % (high - low + 1) + low;
        C = rand() % (high - low + 1) + low;
        D = rand() % (high - low + 1) + low;
        E = rand() % (high - low + 1) + low;
        F = rand() % (high - low + 1) + low;
    
        cout << "Your randomly selected lottery numbers are: " << endl;
        cout << A << " " << B << " " << C << " " << D << " " << E << " " << F << endl;
    
    cin.get();
    }
    I intend to put some Boolean operators in it, so that I don't get any repeating numbers.
    It's not perfect, I know, but I was wondering if I could have made this using an array?
    MrMatt027
    Was here...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You could, if you wanted. What you really want is not random number-picking, but a random shuffle, and in that case you might want an array to hold the shuffle.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    There is an old thread started by yours truly which has a little discussion on methods to achieve this, starting with a basic example by me, then options using the std::set posted by some senior members:

    Allocation method
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User MrMatt027's Avatar
    Join Date
    Nov 2010
    Location
    USA, Florida
    Posts
    21
    Excellent, I'm going to try this out. Thank you both very much.
    MrMatt027
    Was here...

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    here's one that you can use
    Code:
    {
      using namespace std;
      srand(time(NULL)); 
      vector<int> result;
    
      for ( int i = 1 ; i <=50 ; ++i)
        result.push_back(i);
      
      random_shuffle(result.begin() , result.end() );
    }
    Last edited by nimitzhunter; 01-17-2011 at 03:17 PM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question : Passing Array Arguments as Pointers
    By NEMEAN in forum C Programming
    Replies: 1
    Last Post: 12-04-2010, 02:33 PM
  2. Array Question
    By Alecroy in forum C++ Programming
    Replies: 4
    Last Post: 09-26-2010, 03:22 PM
  3. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  4. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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