Boost.Coroutine で乱数のジェネレータ
とりあえず、簡単に。
ジェネレータといいつつ、なんか違うような気がしないでもない。
[ソース]
#include <boost/coroutine/coroutine.hpp> #include <boost/coroutine/all.hpp> #include <vector> #include <iostream> #include <random> #include <functional> #include <cstdint> typedef boost::coroutines::coroutine<int()> coroutine_type; void random_generator(coroutine_type::caller_type& coroutine, int min, int max){ std::mt19937 engine; std::uniform_int_distribution<int> distribution(min, max); while( 1 ){ coroutine(distribution(engine)); } } int main(){ coroutine_type dice(std::bind(random_generator, std::placeholders::_1, 1, 6)); for(int i = 0 ; i < 30 ; ++i){ std::cout << dice().get() << std::endl; } return 0; }
[出力]
1 6 6 1 6 6 2 4 2 1 4 2 2 4 6 6 6 6 6 1 5 6 6 6 1 3 5 5 2 1
んー coroutine のオブジェクトをどう扱うのがいいか。