Here I am rolling dice and storing the results in a map (int for occurrences of a number, long for frequency i.e. occurrences / trials). Here is a typical output:
Please select the number of trials:
100
Dice roll of 1 has 327% outcomes
Dice roll of 2 has 16722170% outcomes
Dice roll of 3 has 327% outcomes
Dice roll of 4 has 14872209% outcomes
Dice roll of 5 has 327% outcomes
Dice roll of 6 has 16724069% outcomes
As you can see the frequencies are all discombobulated. They should total to 1. I have tried messing with the precision but that doesn't appear to be the source of my problem. The code is rather simple. Can anyone pinpoint my problem? Kind regards.
#include <boost/random.hpp>
#include <iostream>
#include <ctime>
#include <boost/Random/detail/const_mod.hpp> // LCG class
#include <map>
using namespace std;
int main()
{
//throwind dice
//Mersenne Twister
boost::random::mt19937 myRng;
//set the seed
myRng.seed(static_cast<boost::uint32_t> (time(0)));
//uniform in range [1,6]
boost::random::uniform_int_distribution<int> six(1,6);
map<int, long> statistics; //structure to hold outcome + frequencies
int outcome; //current outcome
cout << "Please select the number of trials:" << endl;
int trials; //# of trials
cin >> trials;
int oc1; int oc2; int oc3; int oc4; int oc5; int oc6; //outcomes
for (int i = 0; i < trials; ++i)
{
outcome = six(myRng);
if (outcome == 1)
{
oc1++;
statistics[1] = oc1 / trials;
}
if (outcome == 2)
{
oc2++;
statistics[2] = oc2 / trials;
}
if (outcome == 3)
{
oc3++;
statistics[3] = oc3 / trials;
}
if (outcome == 4)
{
oc4++;
statistics[4] = oc4 / trials;
}
if (outcome == 5)
{
oc5++;
statistics[5] = oc5 / trials;
}
if (outcome == 6)
{
oc6++;
statistics[6] = oc6 / trials;
}
}
for (int j = 1; j <= 6; ++j)
cout << "Dice roll of " << j << " has " << statistics[j] << "% outcomes" << endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire