난수를 사용하기 위해서는
srand(( unsigned )time(NULL);
을 해주시면 됩니다. 하는건 자기 마음이겠죠? unsigned int 이렇게 사용하셔도 됩니다.
이렇게 해주셨으면 난수 발생의 범위를 지정해야합니다.
난수 발생의 범위를 지정하는 방법은 이렇습니다.
변수 = rand() % (종료 값 - 시작 값 + 1) + 시작 값
100부터 1000 사이의 난수를 발생시키려면
변수 = rand() % (1000 - 100 + 1) + 100
간단히 하면
변수 = rand() % (901) + 100
이렇게 되지요.
이건 난수 1,000개를 발생시켜서 최소값, 최대값 출력하는 소스.
- #include <stdio.h>
 - #include <stdlib.h>
 - #include <conio.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;
 - }