Desc :

 

void srand( unsigned int seed );

seed - 난수의 초기 값

보통 time(NULL) 사용

 

srand함수에 의해 초기값이 정해지면

int rand( void );

rand함수로 난수를 구한다

 

1부터 150까지의 난수

rand % 150 + 1

// 나머지의 최소값 0 + 1 = 1

나머지의 최대값 149 + 1 = 150


Source Code :

#include <stdio.h>
#include <math.h>

void main()
{
	int i;

	srand((unsigned)time(NULL));

	for (i = 0; i < 5; i++)
	{
		printf("난수%d : %d\n", i, rand());
	}

}

 


Result :

+ Recent posts