Thread: My C++ Homework...Simple Question

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    18

    My C++ Homework...Simple Question

    I need to draw 8 random numbers (from 1 to 4). The numbers can only repeat once. I need there to be two 1s, two 2s, two 3s, and two 4s in random order. I need them put into an array.

    Example Output: 1,3,4,2,4,1,3,2 or 3,1,1,4,3,2,2,4 (I'm sure you get the idea!)

    It would now equal:

    Code:
    intNumbers[8] = {1,3,4,2,4,1,3,2};
    This is a small piece of a bigger project, so I not just on here asking for answers. I really need help with this one. I've been staring at the same 10 lines for several hours now.

    Thanks in advance for any help!
    Tin

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    You can put 1,1,2,2,3,3,4,4 into a <vector> and then call random_shuffle() on the vector. You can then copy() the elements of the vector to your array, although you might consider using a <vector> instead of an array to start with--then you wouldn't need to copy the <vector> to the array.

    random_shuffle() is in <algorithm>.
    Last edited by 7stud; 02-08-2006 at 11:20 PM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you have to use the C style array, random_shuffle will work with that also:

    std::random_shuffle(intNumbers, intNumbers + 8);

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Daved
    If you have to use the C style array, random_shuffle will work with that also:

    std::random_shuffle(intNumbers, intNumbers + 8);
    Darn it.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Thanks a million. Worked like a charm!

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Just so you know, you need to use srand(time(0)) to seed rand(), which is used by random_shuffle(). Otherwise, you will get the exact same sequence every time you run your program--try it.

    time() is in <ctime>

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Luckily I already had that in my code. If I didn't, I would've been stumped. Thanks again!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by 7stud
    Just so you know, you need to use srand(time(0)) to seed rand(), which is used by random_shuffle(). Otherwise, you will get the exact same sequence every time you run your program--try it.
    The implementation of the STL that I use ( g++ ) dosen't make use of the normal rand(), srand() c-functions. It uses an internal random generator that cannot be seeded.
    but it's simple to provide one if needed

    Code:
    struct randomgenerator {
       int operator()( int range ) {
           static int seeded = 0;
           if ( ! seeded ) {
               srand( time(0));
               ++seeded;
           }
           return rand() % range;
       }
    };
    use it this way:
    Code:
        randomgenerator random;
    
        random_shuffle( intNumbers, intNumbers + 8, random );
    Kurt

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    functor! Functor! I see you!

    struct? <shudder>

  10. #10
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by 7stud
    functor! Functor! I see you!

    struct? <shudder>
    What's wrong with that struct. There is nothing to be hidden or protected ?
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM