I am trying to use a boost's random number generator with std::random_shuffle, but it seems that I'm not doing too well, because there are mismatches between expected function signatures.
The first attempt, with Generator, fails because its operator() apparently doesn't take any arguments that random_shuffle requires.Code:#include <iostream> #include <algorithm> #include <boost/random.hpp> int main() { typedef boost::minstd_rand Generator; typedef boost::uniform_smallint<std::size_t> Distribution; Generator gen; Distribution distr(0, 256); boost::variate_generator<Generator, Distribution> gen2(gen, distr); int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; const int array_size = sizeof(array)/sizeof(array[0]); std::random_shuffle(array, array+array_size, gen); std::random_shuffle(array, array+array_size, gen2); for (std::size_t i = 0; i != array_size; ++i) { std::cout << array[i] << ' '; } }
The second attempt, with variate_generator, fails because variate_generator tries to call Distribution::operator()(Generator&, value) whereas the distribution expects operator()(Generator&).
Now, I can work around it by using a proxy object whose operator() does what random_shuffle expects:
However, this still doesn't seem right. For example, by using mod I'm throwing away any advantages that the distribution might provide? And I could use generator alone? Or should I create a new distribution object for each call?Code:struct RandomWrapper { VariateGenerator rng; std::size_t operator() (std::size_t n) {return rng() % n;} };
The random distribution doesn't need to be perfect (no Monte Carlo). Random numbers are simply used to shuffle a bunch of small arrays and to pick random indices from other, slightly larger arrays elsewhere (in a multithreading program, so rand() is not suitable) to produce random puzzles. Efficiency is important though.
Does anyone have any advice how I should proceed?



LinkBack URL
About LinkBacks


