기존의 rand함수와 %연산으로 범쉬를 설정해 난수를 설정 했었으나,
c++11에 관련 기능이 있으니 써보기로 했다.
#include <random>
#include <iostream>
int Util::Random(int min, int includ_max)
{
random_device rn;
mt19937_64 rnd( rn() );
uniform_int_distribution<> range(min, includ_max);
return range( rnd );
}
uniform_int_distribution를 통해 min,max 범위를 설정하고, 값을 리턴하면 끝이다. 간단하다. 여기서 유의해야 할 점은 min, max값이 제외가 아니라 포함이다.
int r = Util::Random(-5,10)
//r [-5 ~ 10] 사이의 정수 반환
https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution
'게임을 만들자 > C++' 카테고리의 다른 글
[알고리즘] 이진트리 만들기 (1) | 2021.05.22 |
---|---|
c++ string tokenizer (0) | 2015.01.09 |
cJSON parsing error using window utf-8 txt file, Remove UTF-8 BOM (0) | 2015.01.08 |
c++ Easing code (0) | 2014.10.16 |
c++ Builder 패턴 (0) | 2014.04.16 |