CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2012
    Posts
    127

    myHistogram = boost/Random + map

    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;
    
    }

  2. #2
    Join Date
    Apr 1999
    Posts
    26,726

    Re: myHistogram = boost/Random + map

    Quote Originally Posted by oteel View Post
    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.
    You've been told on other threads to properly format your code, but you still refuse to do so. Why?

    Also, on your most recent thread, you were asked to debug your code instead of just posting that your program doesn't work correctly. It seems you haven't done that.

    As to your code, I don't know what that boost code does. If you're using it, then you must know what it's supposed to do. All I can understand is that your loop and switch/case could have been written differently:
    Code:
    for (int n = 0; n <5; ++n)
    {
         int ret = uniRng();
         if ( ret >= 1 && ret <= 5)
            myHistogram[ ret ]++;
         else
    	myHistogram[6]++; 
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,507

    Re: myHistogram = boost/Random + map

    I am not sure what you mean by "print screen". Are you basing your belief that the
    code is incorrect based on the random numbers being printed using cout ?
    Code:
    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;
    Notice the 2 calls in red above. They will not generate the same random number.

  4. #4
    Join Date
    Jun 2012
    Posts
    127

    Re: myHistogram = boost/Random + map

    Quote Originally Posted by Philip Nicoletti View Post
    I am not sure what you mean by "print screen". Are you basing your belief that the
    code is incorrect based on the random numbers being printed using cout ?
    Code:
    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;
    Notice the 2 calls in red above. They will not generate the same random number.
    Of course !!! Thanks you for this Idea ! May be you can tell me what I should do in order to get the same set random numbers in this two lines ?

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,507

    Re: myHistogram = boost/Random + map

    Code:
    for (int n = 0; n <5; ++n)
    {
       long random_number = uniRng();
    
       switch ( random_number )
       {
          // all your code here
       }
    
       cout << random_number << endl;
    }

  6. #6
    Join Date
    Jun 2012
    Posts
    127

    Re: myHistogram = boost/Random + map

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    for (int n = 0; n <5; ++n)
    {
       long random_number = uniRng();
    
       switch ( random_number )
       {
          // all your code here
       }
    
       cout << random_number << endl;
    }
    thanks !!!

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width