본문 바로가기
C/☞

[22] 난수 생성 (Random)

by TR. 2020. 7. 14.

time.h

srand(time(0));

rand();

 

# include <Windows.h>
# include <stdio.h>
# include <time.h>				// 헤더파일 추가

void main(){
	srand(time(0));				// 항상변화하는 수인 시간을  seed 값으로 넣어줌 
						// 1970년 1월 1일 0시 0분부터 밀리세컨 단위로 경과된 시간(sec) 반환

	int a = rand();	// rand() : 0~32767
	printf("%d\n", a);

	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);
	printf("%d\n", rand() % 10 + 1);




	system("pause");
}

 


✱ 언어별 헤더파일 확인!

// C
// # include <stdio.h>
// # include <time.h>

// C++
# include <stdio.h>
# include <cstdlib>
# include <ctime>

'C > ' 카테고리의 다른 글

[24] 조건문 : switch - case  (0) 2020.07.15
[23] 가위바위보 게임  (0) 2020.07.14
[21] 반복문 : for  (0) 2020.07.14
[20] 연습문제 : while  (0) 2020.07.14
[19] 반복문 : while  (0) 2020.07.14

댓글