Like Tree1Likes
  • 1 Post By CommonTater

Random number generator/srand

This is a discussion on Random number generator/srand within the C Programming forums, part of the General Programming Boards category; Hi. Im writing a program to do a random walk simulation. In essence it is a random number generator. I'm ...

  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 04: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;
    }
    shansajid likes this.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    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
    21,764
    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 11:51 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,311
    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%.

  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 06:32 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,764
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,764
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07:40 AM

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