TOP > その他
乱数生成器(Random-Number-Generator)
説明
乱数を発生させる。
こどふぉで rand() や random_device を使うと危険。
計算量
- $O(1)$
実装例
- ($y$):= $[0, y)$ で発生させた乱数を返す。
- ($x$, $y$):= $[x, y)$ で発生させた乱数を返す。
struct RandomNumberGenerator {
mt19937 mt;
RandomNumberGenerator() : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
int operator()(int a, int b) { // [a, b)
uniform_int_distribution< int > dist(a, b - 1);
return dist(mt);
}
int operator()(int b) { // [0, b)
return (*this)(0, b);
}
};