Thread: rand() seeded with time but values don't change

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17

    rand() seeded with time but values don't change

    I've used the system time to seed the std random number generator in the past, but for some reason now it is not working. This is on a Mac running OS 10.6.1, compiling with gcc 4.2.1.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    int main() 
    {
    	unsigned int current_time = time(NULL);
      	srand( current_time );
    	std::cout << "\nTime: " << current_time << "\tRandom seed: " << srand  << std::endl;
    
    	return 0;
    }
    The output of this is that the time value changes, but the random seed does not! Here are two example outputs:

    Time: 1253489088 Random seed: 1
    Time: 1253489712 Random seed: 1

    Surely I'm missing something obvious! But what?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You need to call rand().
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by mmfuller View Post
    I've used the system time to seed the std random number generator in the past, but for some reason now it is not working. This is on a Mac running OS 10.6.1, compiling with gcc 4.2.1.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    
    int main() 
    {
        unsigned int current_time = time(NULL);
          srand( current_time );
        std::cout << "\nTime: " << current_time << "\tRandom seed: " << srand  << std::endl;
    
        return 0;
    }
    The output of this is that the time value changes, but the random seed does not! Here are two example outputs:

    Time: 1253489088 Random seed: 1
    Time: 1253489712 Random seed: 1

    Surely I'm missing something obvious! But what?
    You're confused.

    Passing the name of a function to an ostream effectively converts it to a function pointer, which an ostream will print '1' if the pointer is non-null, otherwise '0' (and since any given function is guaranteed to exist (indeed, it wouldn't compile otherwise), the value printed is *always* going to be '1').

    At any rate, the actual seed value in this case is the time value that you passed to the srand function. After that point the implementer of the random library may even discard the value and resort to using some arbitrary (and obscure, as far as your program is concerned) data structure used to generate the next successive value. In other words, if you really need the seed at some point in the future,you'll need to store it in a variable somewhere.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Calling rand() with the same seed will produce the same value. Perhaps I should have stated my question better: why is the value of srand not changing when I give it different values of time?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by mmfuller View Post
    Calling rand() with the same seed will produce the same value. Perhaps I should have stated my question better: why is the value of srand not changing when I give it different values of time?
    Read post #3.

  6. #6
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Quote Originally Posted by mmfuller View Post

    Code:
    	std::cout << "\nTime: " << current_time << "\tRandom seed: " << srand  << std::endl;

    Quote Originally Posted by Sebastiani View Post
    You're confused.

    Passing the name of a function to an ostream effectively converts it to a function pointer, which an ostream will print '1' if the pointer is non-null, otherwise '0' (and since any given function is guaranteed to exist (indeed, it wouldn't compile otherwise), the value printed is *always* going to be '1').
    Quote Originally Posted by mmfuller View Post
    why is the value of srand not changing when I give it different values of time?
    Now maybe you can see what you did wrong.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Sebastiani,
    Thank you. You posted your initial reply at almost the same moment that I posted my reply to Dino. My reply was not a response to your post.

    Your advice was very helpful. I did not realize that sending a function to an ostream would yield a 1.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by mmfuller View Post
    Sebastiani,
    I did not realize that sending a function to an ostream would yield a 1.
    Oh, I thought it was more a case of you didn't even realise that you weren't actually even calling a function there, AND that it's the wrong function anyway. So you've changed
    Code:
    << srand <<
    to
    Code:
    << rand() <<
    now?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing different values of time
    By stellastarr in forum C Programming
    Replies: 3
    Last Post: 02-08-2006, 10:38 AM
  2. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  3. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 04:57 PM
  4. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM