블로그 이미지
fiadot_old

칼퇴근을 위한 게임 서버 개발 방법론에 대한 심도있는 고찰 및 성찰을 위한 블로그!

Rss feed Tistory
Technical Article/펌 2006. 4. 6. 07:58

srand(time(null))

http://www.cplusplus.com/ref/cstdlib/srand.html

  cplusplus.com > reference > cstdlib > srand
srand
<stdlib.h>
  cplusplus.com  
void  rand ( unsigned int seed );

Initialize random number generator.
  Uses seed parameter to set a new starting point for generating random numbers with rand.
  If seed is set to 1 the generator is reinitialized to its initial value as before any call to rand or srand.
  In order to generate true random numbers it is suggested to use as seed a value that changes often, like the one returned by time function included in <time.h> (the number of seconds elapsed since newyear 1970).

Parameters.

seed
An integer value to be used as starting point with the pseudo-random number generator algorithm.

Return Value.
  (none)

Portability.
  Defined in ANSI-C.

Example.

/* rand/srand example */#include <stdio.h>#include <stdlib.h>#include <time.h>int main (){  /* initialize random generator */  srand ( time(NULL) );  /* generate some random numbers */  printf ("A number between 0 and 100: %d\n", rand()%100);  printf ("A number between 20 and 30: %d\n", rand()%10+20);  return 0;}
Output:
A number between 0 and 100: 93
A number between 20 and 30: 21

See also.
  rand

,
TOTAL TODAY