srand cast

This is a discussion on srand cast within the C Programming forums, part of the General Programming Boards category; Why should I cast time when I call srand? I've been told to do it but I don't know why. ...

  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
    20,968
    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.
    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

  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
    Aia
    Aia is offline
    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 08:20 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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().
    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
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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

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