Programming/C++
C++ 난수 생성 라이브러리
ahhang0k
2021. 4. 9. 16:40
rand()
- 난수를 생성하는 함수
- 0부터 RAND_MAX까지의 난수를 생성
#include <iostream>
#include <cmath>
#include <ctime>//함수 time의 헤더파일
#include <cstdlib>//rand()의 헤더파일
using namespace std;
// 0에서 9까지의 n개의 난수를 화면에 출력한다.
void get_random( int n )
{
int i;
for( i = 0; i < n; i++ )
cout << rand() << endl;
cout << rand()%100+1 <<endl; //rand()%100+1 1부터 100까지 표현
}
int main()
{
// 일반적으로 난수 발생기의 시드(seed)를 현재 시간으로 설정한다.
// 현재 시간은 수행할 때마다 달라지기 때문이다.
srand( (unsigned)time( NULL ) ); //컴파일 시간부터 다시 수행, 랜덤값엔 필수
get_random( 10 );
return 0;
}
로또 번호 난수 생성
<참고>
issac-min.tistory.com/28 라이브러리 cmath함수 알려주는 곳
docs.microsoft.com/ko-kr/cpp/c-runtime-library/reference/rand?view=msvc-160