time
원형 time_t time(time_t *timer);
헤더파일 time.h
1970년 1월 1일 이후 경과한 초를 리턴한다.
#include <stdio.h>
#include <time.h>
void main()
{
time_t t;
time(&t);
printf("time : %ld\n",t);
}
rand
원형 int rand(void);
헤더파일 stdlib.h
무작위 난수를 만들어 리턴한다.
srand
원형 void srand(unsigned seed);
헤더파일 stdlib.h
seed가 다르면 다른 패턴의 난수가 생성된다.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int i;
time_t t;
time(&t);
printf("time : %ld\n",t);
srand(100); // 시드값 100일때
//srand(777); // 시드값 777일때
for(i=0;i<15;i++)
{
printf("%d : %d\n", i+1,rand());
}
}
프로그램 실행중 실시간 난수를 생성하기
시간을 srand()에 넣어서 난수를 생성한다.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main()
{
int i;
time_t t;
time(&t);
printf("time : %ld\n",t);
srand(t);
for(i=0;i<15;i++)
{
printf("%d : %d\n", i+1,rand());
}
}
'프로그래밍 > C 함수 레퍼런스' 카테고리의 다른 글
floor, ceil (내림, 올림, 반올림) pow(거듭 제곱) C 함수 레퍼런스 (0) | 2014.06.19 |
---|---|
malloc calloc realloc (동적 메모리 할당) C 함수 레퍼런스 (0) | 2014.06.19 |
strcpy strncpy (문자열 복사) C 함수 레퍼런스 (0) | 2014.06.19 |
strcat strncat(문자열 연결) C 함수 레퍼런스 (0) | 2014.06.19 |
strlen (문자열 길이) C 함수 레퍼런스 (0) | 2014.06.19 |