1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <ctime>
#include <iomanip>
#include <random> // PART 2
int main()
{
// ALTERNATIVE 1
const int LIMIT{16000};// NEED AT LEAST ~500 - ?ROUNDING
int array[LIMIT];
for(int i = 0; i < LIMIT; i++)
array[i] = -1;
double distribution[]
{ 0.200, 0.200, 0.140, 0.100, 0.088, 0.088, 0.046, 0.046, 0.046, 0.046 };
int check_count[10]{0};
srand( time(nullptr) );
int count{0};
int cell{0};
for(int number = 0; number < 10; number++)
{
for(int no_shots = 0; no_shots < LIMIT * distribution[number]; no_shots++)
{
cell = rand() % LIMIT;
while( array[cell] >= 0 )
{
cell++;
cell = cell % LIMIT;
}
array[cell] = number;
check_count[number]++;
count++;
}
}
int col_count{0};
for(auto i:array)
{
std::cout << std::setw(4) << std::left << i ;
col_count++;
if(col_count % 20 == 0)
std::cout << '\n';
}
std::cout << "\nCOUNT = " << count << '\n';
for (int i = 0; i < 10; i++)
{
std::cout << i << std::setw(5) << std::right <<check_count[i] << '\n';
}
// ALTERNATIVE 2
const int nrolls = LIMIT; // number of experiments
const int nstars = 100; // maximum number of stars to distribute
std::default_random_engine generator;
std::discrete_distribution<int> distribution_2
{ 200, 200, 140, 100, 88, 88, 46, 46, 46, 46 };
int p[10]={};
for (int i=0; i<nrolls; ++i) {
int number = distribution_2(generator);
++p[number];
}
std::cout << "a discrete_distribution:" << std::endl;
for (int i=0; i<10; ++i)
std::cout << i << ": " << p[i] /*std::string(p[i]*nstars/nrolls,'*')*/ << std::endl;
return 0;
}
|