random number problems

This is a discussion on random number problems within the C Programming forums, part of the General Programming Boards category; ok, I feel like what I'm missing is something simple but i"m not seeing it, I'm trying to write a ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    1

    random number problems

    ok, I feel like what I'm missing is something simple but i"m not seeing it, I'm trying to write a program that generates a random number between 1 and 2 and do that x number of times where x is the input from the user. The way this works is it keeps printing the same number over and over. Like if it's 2 then it shows that 2 is generated each time.

    Code:
    int twoside(void)
    {
      int i, n;
      n = 0;
    
      do{
      srand(time(NULL));
      i = (rand() % (2) + 1);
    
        printf("The number rolled is: %d\n", i);
    
             n++;
    
      }while (n < num); // num is user input for number of times the number is generated.
    
      return i;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Call srand() just once, not every iteration through the loop.

    Edit: When I say just once, it should be just once in the entire program, not just in the function, since time_t typically has 1 second resolution (not guaranteed though) so if you had srand() in the function, and called the function more than once in a second, you would get the same sequence of values in the successive function calls.

    http://en.wikipedia.org/wiki/Time_t
    Last edited by robatino; 07-04-2007 at 11:37 PM.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    63
    Code:
    int GetRandomValues(int nTimes, int low, int high)
    {
        if(nTimes)
        {
                  int i = 0;
                  int randomNumber = 0;
                  int width = high - low + 1;
                  if(width < 0)
                           width = -width;
                  //Initialise generator.
                  srand(time(NULL));
                  
                  for(i = 0; i < nTimes; randomNumber = (rand() % width) + low, printf("Random Number Generated: %d\n", randomNumber), i++);
                  if(i == nTimes)
                       return 0;
                  else
                  {
                      printf("Error in loop.\n");
                      return -1;
                  }
        }                  
        else
        {
            printf("Please insure that we have a non zero number of times.\n");
            return -2;
        }
    }
    The above code generates nTimes the numbers you want. Check it and make one for yourself. If you encounted any problems tell us.

    In main() call the fucntion like that:

    GetRandomValues(10, 1,2);

    Happy Codings.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    It looks like your num is a global variable. Avoid using global, they are not good. Send them as a paramter which makes it more neater.

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rapid random number generation problem
    By Newton in forum C Programming
    Replies: 17
    Last Post: 09-19-2008, 03:08 PM
  2. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  3. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  4. Random Number Generator
    By Ikurik in forum C++ Programming
    Replies: 16
    Last Post: 08-17-2003, 08:34 PM
  5. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 03:40 PM

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