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

This is a discussion on rand() seeded with time but values don't change within the C++ Programming forums, part of the General Programming Boards category; I've used the system time to seed the std random number generator in the past, but for some reason now ...

  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
    Katy, Texas
    Posts
    2,309
    You need to call rand().
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,305
    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, 09:38 AM
  2. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 06: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

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