Skip to content

Instantly share code, notes, and snippets.

@JanOleA
Last active December 23, 2020 18:51
Show Gist options
  • Save JanOleA/3e5ff4d98b9561face8ca7e83533f6c6 to your computer and use it in GitHub Desktop.
Save JanOleA/3e5ff4d98b9561face8ca7e83533f6c6 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <random>
#include <numeric>
using namespace std;
int vectorsum(vector<int> vec) {
return accumulate(vec.begin(), vec.end(), 0);
}
double vectoravg(vector<double> vec) {
double s = 0;
for (double v : vec) {
s += v;
}
return s / (double)vec.size();
}
int main() {
// Setting up the PRNG
random_device rd;
mt19937 gen(rd());
uniform_real_distribution<> dis(0.0, 1.0);
int goal = 10;
double chance = 0.045;
int attempts = 22;
int simnums = 10000;
double flip;
vector<double> individual_droprates;
vector<double> entire_attempt_droprates;
for (int i = 0; i < simnums; i++) {
vector<double> individual_results;
vector<int> all_runs;
for (int j = 0; j < attempts; j++) {
vector<int> this_attempt;
while (vectorsum(this_attempt) < goal) {
flip = dis(gen);
if (flip < chance) {
this_attempt.push_back(1);
all_runs.push_back(1);
} else {
this_attempt.push_back(0);
all_runs.push_back(0);
}
}
individual_results.push_back(((double)vectorsum(this_attempt))/((double)this_attempt.size()));
}
individual_droprates.push_back(vectoravg(individual_results));
entire_attempt_droprates.push_back(((double)vectorsum(all_runs))/((double)all_runs.size()));
}
cout << endl;
cout << vectoravg(individual_droprates) << endl;
cout << vectoravg(entire_attempt_droprates) << endl;
cout << chance << endl;
cout << vectoravg(entire_attempt_droprates)/chance*100 - 100 << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment