난수 발생의 범위를 지정하는 방법, 요렇게
변수 = rand() % (종료 값 - 시작 값 + 1) + 시작 값
100부터 1000 사이의 난수를 발생시키려면
변수 = rand() % (1000 - 100 + 1) + 100
간단히 하면
변수 = rand() % (901) + 100
이렇게 되지요.
이건 난수 1,000개를 발생시켜서 최소값, 최대값 출력하는 소스.
#include <stdio.h>
#include <time.h>
int main()
{
int a;
int start,end;
int max,min;
int count;
srand((unsigned)time(NULL));
printf("시작 : ");
scanf("%d",&start);
printf("종료 : ");
scanf("%d",&end);
min=end;
max=start;
for(count=0;count<1000;count++)
{
a=rand() % (end - start + 1) + start;
printf("%4d ",a);
if(min > a)
min=a;
if(max < a)
max=a;
}
printf("\n최소 : %d",min);
printf("\n최대 : %d",max);
getch();
return 0;
}
널려 있는 정보지만, 아니 뭐 그냥 그렇다구요. ㅜㅜ
Trackback Address >> http://zfanta.com/trackback/335
-
Subject: 추억6. MSX-BASIC과 HU-BASIC의 RND() 함수의 차이
Tracked from BLUE'nLIVE 2008/02/15 17:53 deleteSYNOPSIS #include <stdlib.h> int rand(void) DESCRIPTION The rand() function returns a pseudo-random integer between 0 and RAND_MAX. If no seed value is provided, the rand() function is auto-matically seeded with a value of 1. RETURN VALUE The rand() fun..
옛날 생각 나서 적은 글을 트랙백 걸었습니다.
난수 발생… 은근히 재미있는 테마죠.