srand(time(NULL));

This is a discussion on srand(time(NULL)); within the C Programming forums, part of the General Programming Boards category; hi , i just want to ask the use of time and NULL here ....... and also , are the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    3

    srand(time(NULL));

    hi , i just want to ask the use of time and NULL here .......

    and also , are the random numbers generated this way really random ????

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>hi , i just want to ask the use of time and NULL here .......
    Read this.

    >>are the random numbers generated this way really random ????
    No.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It seeds the random number generator with the current system time. This only needs to be done the once.
    You then use rand() to get random numbers. They are reasonably random,not perfect but computers can only do as they are told to and cannot just think of a number for themselves so they will never produce truly random numbers.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
       /* Seed the random-number generator with current time so that
        * the numbers will be different every time we run.
        */
       srand( (unsigned)time( NULL ) );
    The NULL means that the value (the time in seconds) isn't stored anywhere.

    >>are the random numbers generated this way really random ????

    The numbers generated are known as pseudo-random numbers.

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    i recall a linux module whose purpose was to support entropy-randomness collection from a chip on the motherboard... maybe it's a specialized-applicaitons thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. attacking sequence
    By LouDu in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-11-2003, 06:24 PM
  2. Turn based battle demo
    By Kirdra in forum Game Programming
    Replies: 0
    Last Post: 09-13-2002, 02:01 PM

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