Thread: Random number generator/srand

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Dublin
    Posts
    5

    Random number generator/srand

    Hi.

    Im writing a program to do a random walk simulation. In essence it is a random number generator. I'm seeding from the system clock before decalring a loop using the rand() generator.

    My question is....

    If im seeding from the system clock, shouldnt the seed be different each time the program is compilled?

    Here's how I innisiate the seed (srand) using the time.h library:

    time_t curr_time;

    srand( (unsigned) time(&curr_time));

    Basically I'm defining the system time to be a variable called curr_time then assign the seed to be that value. If that is true srand should be different everytime the program is compilled but i have found it is not YET........the rand function is.........Im stumped.

    Any help much appreciated
    Last edited by Dave.b; 11-26-2010 at 05:13 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try your seed as simply srand(time(0)); That will give you a different seed value every second.
    Call it once at the beginning of your program and use rand() after.

    Code:
    int main(void);
      { 
      int i;
      srand(time(0));
    
      for (1 = 0; i < 100; i++)
        printf("%d   ", rand() % 100);
    
      return 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Make sure you included <time.h>, <stdlib.h> so that time returns the proper type.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Dublin
    Posts
    5
    Hi guys,

    thanks for the replies. I have the time.h and stdlib.h library's in there and I tried the srand(time(0)); suggestion. It returns the same value as when I used my method and each run of the loop.....confusing!!

    I know it is itterating as the rand() is different each time but cant figure out why it wont print a different srand at each stage.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try this fixed version of CommonTater's example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        srand(time(0));
    
        for (i = 0; i < 100; i++)
            printf("%d ", rand() % 100);
    
        return 0;
    }
    Also, read Prelude's tutorial on using rand().
    Last edited by laserlight; 11-29-2010 at 12:51 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Dublin
    Posts
    5
    Thanks Laserlight for your post but unfortuneatly it doesnt help.

    Just to clarify, i have a fully functioning program that is returning the correct values. There appears to be nothing wrong with my program except.......

    when I print the seed it doesnt change BUT the random number does. This is suggesting that the seed is indeed changing but the print function some how wont when I compile. I've checked the system time using C and it is changing.

    Ive also noticed one other strange thing. Depending on where in the program I place the printf statement the sedd is different but remains unchanged on each compile of the program.

    Maybe I can submit my program for review. Not sure how to do it though....I assume between [QUOTE][QUOTE].....?

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    time() is called at run time, not compile time.

    Recompiling the program will not affect the value returned - the system clock determines that (at least, within the granularity of the time() function - two calls close together may produce the same result).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Dublin
    Posts
    5
    Hi Grumpy,

    Thanks, This sounds like a winner...I dont know the difference between run and compile time but ill check..........but if this is true and i print system time and seed side by side...both should change not just the system time?.........this is whats happening
    Last edited by Dave.b; 11-30-2010 at 07:32 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave.b
    Maybe I can submit my program for review. Not sure how to do it though....I assume between [quote][quote].....?
    Not quite: between [code][/code] bbcode tags.

    Quote Originally Posted by Dave.b
    but if this is true and i print system time and seed side by side...both should change not just the system time?
    You are directly using the return value of time(0) as the seed, so it really is the same thing. In your original code, the curr_time variable is useless: all it does is store the result of the call to time(), but you are already directly using that result as an argument to srand().

    Did you read Prelude's tutorial?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Dublin
    Posts
    5
    Thanks laserlight,

    Yup I read the tutorial but It didnt answer my question or else it did and i dont get it.......

    You are directly using the return value of time(0) as the seed, so it really is the same thing.
    Would you mind elaborating on this please? It seems to me that yes it will be the same except for when the program is compilled again... (sorry grumpy still havnt got that compile run thing sorted)...as the system clock will have changed by then?

    As for:

    In your original code, the curr_time variable is useless: all it does is store the result of the call to time(), but you are already directly using that result as an argument to srand().
    Thats true, just good practice where im doing this for.

    Thanks again

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Dave.b
    Would you mind elaborating on this please?
    The argument to srand is the seed for the pseudorandom number generator, e.g., if you call srand(1); the seed will be 1.

    Quote Originally Posted by Dave.b
    It seems to me that yes it will be the same except for when the program is compilled again... (sorry grumpy still havnt got that compile run thing sorted)...as the system clock will have changed by then?
    Well, here is a test program:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
        printf("%lu\n", (unsigned long)time(0));
        return 0;
    }
    Compile this program and run it once. Record the output. Make yourself a cup of coffee and finish drinking it. Without compiling it again, run the program again. Is the output the same? Now, go for a jog and take a shower when you return. After the shower, without compiling your program again, run it. Is the output the same?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  2. Simple XOR Program
    By dolfaniss in forum C Programming
    Replies: 8
    Last Post: 05-24-2010, 01:27 PM
  3. random number generator help
    By mayoussa89 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2010, 07:26 AM
  4. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  5. random number between negative and positive number
    By anomaly in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2003, 08:40 AM