본문 바로가기

랜덤함수3

[C언어] 행운의 숫자 맞추기 게임 랜덤 함수를 이용한 행운의 숫자 맞추기 게임입니다. #include #include #include int main() { int i=1, num, res, random; srand(time(NULL)); random=rand()%10; printf("행운의 숫자를 맞춰보세요!!!\n"); do{ printf("%d round:", i), scanf("%d", &num); if(num==random) { printf("맞췄습니다!!!\n"); printf("당신은 럭키 가이!!!\n"); res=0; } else if(i==5) { printf("다시 도전 합시다!!\n"); res=0; } else { printf("아닙니다!!\n"); res=1; } i++; }while(res==1); return.. 2020. 7. 23.
로또 번호 생성기 간단히 만든 로또 번호 생성기입니다. 랜덤 함수를 이용하여 1~45까지 중 6개의 정수를 출력합니다. 같은 숫자가 출력되면 안되므로 조건으로 추가하였습니다. 여기에 다양한 옵션들을 더하면 유용한 로또 번호 생성기가 될 것입니다. #include #include #include int main() { int i, num, pre_num=0, cnt=0; srand(time(NULL)); printf("로또 번호 생성기\n"); while(cnt 2020. 6. 25.
[C언어] 랜덤 함수, 난수 생성 함수, rand() 랜덤(random) 함수, 난수 생성 함수 난수(random number)는 특정한 규칙을 가지지 않은 수를 말합니다. C언어에서 제공하는 난수를 생성하는 함수에는 rand()함수가 있습니다. rand()함수를 사용하기 위해서는 stdlib.h 헤더 파일을 포함시켜야 합니다. rand()함수는 0~ 32,767의 수 중 무작위로 하나의 수를 반환합니다. 아래는 rand()함수를 이용하여 난수 1개를 출력하는 프로그램입니다. #include #include int main() { printf("%d", rand()); return 0; } 변수를 이용하여 rand()함수를 사용할 수도 있습니다. #include #include int main() { int random; random=rand(); prin.. 2020. 6. 25.