|
-
July 5th, 2012, 02:15 AM
#1
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;
}
-
July 5th, 2012, 03:10 AM
#2
Re: myHistogram = boost/Random + map
 Originally Posted by oteel
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
-
July 5th, 2012, 05:10 AM
#3
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.
-
July 11th, 2012, 05:50 AM
#4
Re: myHistogram = boost/Random + map
 Originally Posted by Philip Nicoletti
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 ?
-
July 11th, 2012, 06:04 AM
#5
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;
}
-
July 11th, 2012, 06:14 AM
#6
Re: myHistogram = boost/Random + map
 Originally Posted by Philip Nicoletti
Code:
for (int n = 0; n <5; ++n)
{
long random_number = uniRng();
switch ( random_number )
{
// all your code here
}
cout << random_number << endl;
}
thanks !!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|