bignum_t pick_random_simplex(const std::vector<bignum_t>& sxs ) { static base_generator_type generator(static_cast<unsigned int>(std::time(0))); typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type; gen_type die_gen(generator, distribution_type(0, sxs.size())); boost::generator_iterator<gen_type> die(&die_gen); return sxs[*die]; }
fixnum_t pick_random_neighbor( void ) { static base_generator_type generator(static_cast<unsigned int>(std::time(0))); typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type; gen_type die_gen(generator, distribution_type(0, 3)); boost::generator_iterator<gen_type> die(&die_gen); return *die; }
int RNG::roll(int low, int high) { typedef boost::mt19937 base_generator_type; base_generator_type generator( rand() + std::time(0) ); typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type; gen_type die_gen(generator, distribution_type(low, high)); boost::generator_iterator<gen_type> die(&die_gen); return *die; }
// This is a reproducible simulation experiment. See main(). void experiment(base_generator_type & generator) { // Define a uniform random number distribution of integer values between // 1 and 6 inclusive. typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type; gen_type die_gen(generator, distribution_type(1, 6)); // If you want to use an STL iterator interface, use iterator_adaptors.hpp. boost::generator_iterator<gen_type> die(&die_gen); for(int i = 0; i < 10; i++) std::cout << *die++ << " "; std::cout << '\n'; }
// This is a reproducible simulation experiment. See main(). void experiment(base_generator_type & generator) { // Define a uniform random number distribution of integer values between // 1 and 6 inclusive. typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type&, distribution_type> gen_type; gen_type die_gen(generator, distribution_type(1, 6)); #if !defined(__SUNPRO_CC) || (__SUNPRO_CC > 0x530) // If you want to use an STL iterator interface, use iterator_adaptors.hpp. // Unfortunately, this doesn't work on SunCC yet. boost::generator_iterator<gen_type> die(&die_gen); for(int i = 0; i < 10; i++) std::cout << *die++ << " "; std::cout << '\n'; #endif }
void random_boost() { typedef boost::minstd_rand base_generator_type; //typedef boost::mt19937 base_generator_type; base_generator_type baseGenerator(static_cast<unsigned int>(std::time(NULL))); // uniform (integer) { typedef boost::uniform_int<> distribution_type; typedef boost::variate_generator<base_generator_type &, distribution_type> generator_type; generator_type die_gen(baseGenerator, distribution_type(1, 6)); for(int j = 0; j < 10; ++j) { //baseGenerator.seed(42u); // caution: generate repetitive sample for(int i = 0; i < 10; ++i) std::cout << die_gen() << ' '; std::cout << std::endl; } std::cout << std::endl; } // uniform (real) { typedef boost::uniform_real<> distribution_type; typedef boost::variate_generator<base_generator_type &, distribution_type> generator_type; baseGenerator.seed(static_cast<unsigned int>(std::time(NULL))); generator_type uni_gen(baseGenerator, distribution_type(0, 1)); for(int j = 0; j < 10; ++j) { //baseGenerator.seed(42u); // caution: generate repetitive sample for (int i = 0; i < 10; ++i) std::cout << uni_gen() << ' '; std::cout << std::endl; } std::cout << std::endl; } // normal { typedef boost::normal_distribution<> distribution_type; typedef boost::variate_generator<base_generator_type &, distribution_type> generator_type; const double mean = 1000.0; const double sigma = 100.0; generator_type normal_gen(baseGenerator, distribution_type(mean, sigma)); for(int j = 0; j < 10; ++j) { //baseGenerator.seed(42u); // caution: generate repetitive sample for(int i = 0; i < 10; ++i) std::cout << normal_gen() << ' '; std::cout << std::endl; } std::cout << std::endl; } // //base_generator_type saved_generator = baseGenerator; //assert(baseGenerator == saved_generator); // //std::ofstream stream("rng.saved", std::ofstream::trunc); //stream << baseGenerator; }