The project is compile but something wrong in switch case loop. The amount of numbers counting uncorrect. Please take a look to print screen. What I did wrong . Many thanks in advance.
Code:// #include <boost/random.hpp> // Convenience header file #include <iostream> #include <ctime> // std::time #include <boost/Random/detail/const_mod.hpp> // LCG class #include <map> using namespace std; template<class PRNG, class Dist> inline boost::variate_generator<PRNG&, Dist> make_gen(PRNG & rng, Dist d) { return boost::variate_generator<PRNG&, Dist>(rng, d); } int main() { // Get random number generator boost::mt19937 rng; // Set the seed. rng.seed(static_cast<boost::uint32_t> (std::time(0))); boost::random::uniform_int_distribution<int> six(1,6); rng.seed(static_cast<unsigned int> (std::time(0))); boost::variate_generator<boost::mt19937&, boost::random::uniform_int_distribution<int> > uniRng(rng, six); map<int, long> myHistogram; myHistogram[1] = 0; myHistogram[2] = 0; myHistogram[3] = 0; myHistogram[4] = 0; myHistogram[5] = 0; myHistogram[6] = 0; for (int n = 0; n <5; ++n) { switch (uniRng()) { case 1: myHistogram[1]++; break; case 2: myHistogram[2]++; break; case 3: myHistogram[3]++; break; case 4: myHistogram[4]++; break; case 5: myHistogram[5]++; break; default: myHistogram[6]++; break; } cout << uniRng() << endl; } for (auto i = myHistogram.cbegin(); i != myHistogram.cend(); ++i) { cout <<"case:" << i->first << " frequency: " <<i->second<<" "<< endl; } return 0; }




Reply With Quote