#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <vector>
static bool isGet(int captureProbability)
{
const int r = rand() % 100;
return r < captureProbability;
}
int main()
{
const int probablility = 12; /*捕獲率*/
const int captureNum = 50; /*1セット捕獲率*/
const int trialNum = 10000; /*試行回数*/
srand(time(0));
std::vector<int> data(trialNum);
for(int i = 0 ; i < trialNum ; ++i)
{
int capture = 0;
for(int j = 0 ; j < captureNum ; ++j)
{
if(isGet(probablility))
++capture;
}
data[i] = capture;
}
int sum = 0;
for(int i = 0 ; i < trialNum ; ++i)
{
sum += data[i];
}
const double average = static_cast<double>(sum) / static_cast<double>(trialNum);
double scattering = 0.0;
for(int i = 0 ; i < trialNum ; ++i)
{
const double dif = average - static_cast<double>(data[i]);
scattering += dif * dif;
}
scattering /= trialNum;
const double standardDeviation = sqrt(scattering);
std::cout << "捕獲率12%のモンスターを50匹倒したとき、" << std::endl;
std::cout << "68%の確率で、" << average - standardDeviation << "~" << average + standardDeviation << "匹捕獲できる" << std::endl;
std::cout << "95%の確率で、" << average - standardDeviation * 2.0 << "~" << average + standardDeviation * 2.0 << "匹捕獲できる" << std::endl;
return 0;
}