C언어 수업정리/3월 수업정리
3월 29일 rand함수와 srand함수(난수생성)
송시혁
2013. 3. 29. 15:51
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main() { int i; long seed; printf("1에서 1000 사이의 난수 5개의 생성\n"); for( i = 0; i <= 5; i++) { printf("%7d", (rand() % 1000) + 1); } printf("\n"); printf("\nRandom Number Generation Using Seed Value\n");
seed = time (NULL); srand (seed); for(i = 0; i <= 5; i= i+1) { printf("%7d", (rand() % 1000) +1); }
printf("\n");
}
|

이것은 로또번호 프로그램.
rand = 랜덤하게 숫자를 뽑아낸다.
srand = 랜덤으로 뽑아내되, 실행할 때마다 값이 달라니다.
srand(time NUll)
time(NULL) = 1970년 1월 1일부터 시간을 계산하여
초로 바꾼놈이다.
#include <stdio.h> #include <stdlib.h> #include <time.h>
int main() { int i; long seed; printf("\n로또 번호는 :\n");
seed = time (NULL); srand (seed); for(i = 0; i < 6; i= i+1) { printf("%7d", (rand() % 45) +1); }
printf("\n");
}
|