Can't exit this loop

This is a discussion on Can't exit this loop within the C Programming forums, part of the General Programming Boards category; I'm trying to create 2 random numbers. Both numbers must be between 0 and 2 but they cannot be equal ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Angry Can't exit this loop

    I'm trying to create 2 random numbers. Both numbers must be between 0 and 2 but they cannot be equal to each other. If the first random number is 1, then the second random number must be either 0 or 2 or the program will not exit the loop. For some reason however, I'm ending up with the same number both times thus keeping me in an infinate loop. Here's my code:
    do
    { srand((unsigned)time(NULL)); //1st pick
    pick1 = rand() % 3;
    srand((unsigned)time(NULL)); //second pick
    pick2 = rand() % 3;
    }while (pick1 == pick2);
    How can I fix this? Thanks in advance.
    -Goalie35

  2. #2
    Unregistered
    Guest
    "srand" stands for "seed random number generator". You need only call it once.
    Code:
    srand( time( 0 ) );
    do
    {
       pick1 = rand() %3;
       pick2 = rand() %3;
    } while( pick1 == pick2 );
    Quzah.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2

    Talking ok, one more question about this

    I didn't mention this earlier because I didn't think it would cause a problem but this random number generator is in the middle of a loop I created. Unfortunately, when the loop is done(lets say it looped 10 times and was supposed to create 10 random numbers for both variables)the value never changes. If the "pick1" variable had a value of 2 on the first pass, it keeps that value for all ten passes. I know how to completely flush all values of all variables in the i/o stream however I only want to flush my "pick1" and "pick2". How do I do this.
    Thanks in advance.
    -Goalie35

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Post your code.

    Do you call srand() once, and ONLY once? Calling srand() within a loop will cause problems. Call it (or randomize()) at the very beginning, and then never call it again.

  5. #5
    Unregistered
    Guest
    randomize() is not an ANSI function.

    Right after you declare your variables in main, use:
    srand( time( 0 ) );

    You never need to do it again.
    If you reseed rand in the same loop, with same value, you're likely going to end up with the first and second items being the same:

    srand( time( 0 ) );
    x = rand()%3;
    srand( time( 0 ) );
    y = rand()%3;
    y == 1 //more than likely due to how fast these instructions are carried out

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rewriting a for loop as a while/do-while loop
    By Ashfury in forum C++ Programming
    Replies: 7
    Last Post: 04-27-2007, 02:20 PM
  2. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  3. Trying to figure out a simple loop....
    By chadsxe in forum C++ Programming
    Replies: 9
    Last Post: 01-05-2006, 12:31 PM
  4. I need help as soon as possible.
    By hyrule in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 04:49 PM
  5. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM

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