Thread: srand cast

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    srand cast

    Why should I cast time when I call srand? I've been told to do it but I don't know why. Won't the number from time be converted to unsigned automatically?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        int i;
    
        /* Why the cast? */
        srand((unsigned)time(NULL));
    
        for ( i = 0; i < 10; ++i )
        {
            printf("%d\n", rand());
        }
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That cast may not even be correct. I suggest that you read Prelude's article on Using rand() and also her article on random numbers.
    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

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    I think it's out of my league. But that still doesn't answer my question. This is all I found.
    The first issue is that time_t is a restricted type, and may not be meaningfully converted to unsigned int. That is not a terribly big issue as I have yet to see a system where it would fail to work correctly, and I do not know anyone who has either.
    She used the cast too but didn't say why it's needed.

  4. #4
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Banana Man View Post
    I think it's out of my league. But that still doesn't answer my question.
    This is the prototype of srand.
    Code:
    void srand ( unsigned int seed );
    The parameter that srand accepts is an unsigned int.

    This is the prototype of time.
    Code:
    time_t time ( time_t * timer );
    It returns a value of type time_t.

    This is what is said about type time_t in wikipedia.
    The time_t datatype is a data type in the ISO C library defined for storing system time values. Such values are returned from the standard time() library function. This type is a typedef defined in the standard <time.h> header. ISO C defines time_t as an arithmetic type, but does not specify any particular type, range, resolution, or encoding for it. Also unspecified are the meanings of arithmetic operations applied to time values.
    According to this, time_t could be implemented as a signed or unsigned int or long or etc...

    In an intent to make sure that srand will get the right type value, people cast it.
    Last edited by Aia; 01-08-2008 at 09:20 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    She used the cast too but didn't say why it's needed.
    She gave the common example of how people used it, and stated that it might not be portable. What she recommended instead is to hash the bytes of the time_t returned by time().
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >She used the cast too but didn't say why it's needed.
    It's not needed (except possibly to silence a warning). Either way the value will be converted to unsigned int, but either way, that conversion may fail miserably due to the lack of restrictions on time_t. If time_t is a double, for example, and the value is outside the bounds of unsigned int, the behavior is undefined with or without the cast.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed