Sprout.Random でコンパイル時乱数
少し使ってみました。
[ソース]
#include <sprout/random.hpp> #include <iostream> int main(){ namespace random = sprout::random; static constexpr random::hellekalek1995 engine{}; // 100 ~ 999 static constexpr random::uniform_int_distribution<int> dist1(100, 999); static_assert(dist1(engine) == 984, ""); // 2回目も同じ結果 static_assert(dist1(engine) == 984, ""); static_assert(dist1(engine) == dist1(engine), ""); // 0.0 ~ 1.0 static constexpr random::uniform_real_distribution<double> dist2(0.0, 1.0); // static_assert(dist2(engine) == 0.982829, ""); static_assert(sprout::abs(dist2(engine) - 0.982829) < 0.001, ""); return 0; }
基本的には標準ライブラリの
ただし、constexpr なので常に同じ値の乱数が返ってくるので複数の乱数値を生成する場合にはもう少し工夫して使用する必要があります。
しかし、標準ライブラリのようにジェネレータもいくつか用意されているぽいし、ぱない。