본문 바로가기

C, C++

[C++] 랜덤 함수

C++11 표준에서 제공하는 랜덤 엔진 메르센 트위스터 는 rand() 함수보다 빠르며, 분포도가 고르며 전역성을 띄지 않습니다.

때문에 rand() 함수를 이제 아래와같이 mt199937 클래스로 대체하여 사용합니다.

 

int getRandom(int min, int max)
{
 std::random_device rd;
 std::mt19937 rEngine(rd());
 std::uniform_int_distribution<> dist(min, max);
 return static_cast<int>(dist(rEngine));
}

float getRandom(float min, float max)
{
 std::random_device rd;
 std::mt19937 rEngine(rd());
 std::uniform_real_distribution<> dist(min, max);
 return static_cast<float>(dist(rEngine));
}

'C, C++' 카테고리의 다른 글

[C++] 싱글톤 패턴  (0) 2015.06.04
[C언어] 10진수를 2진수데이터로 바꾸기  (0) 2015.05.28