Skip to content

Instantly share code, notes, and snippets.

@Ernest314
Last active September 18, 2018 17:08
Show Gist options
  • Save Ernest314/4a25e873d365df4ede2b70519a9502af to your computer and use it in GitHub Desktop.
Save Ernest314/4a25e873d365df4ede2b70519a9502af to your computer and use it in GitHub Desktop.
A quick simulation for how many cards need to be crafted to make a specific Darkmoon Deck.
#include "pch.h"
#include <chrono>
#include <iostream>
#include <random>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main() {
cout << "Simulates the number of cards needed to craft a darkmoon deck." << endl;
unsigned long long seed =
std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937 rngesus(seed);
cout << "seed: " << seed << endl;
std::uniform_int_distribution<int> dist_deck(0, 3);
std::uniform_int_distribution<int> dist_card(0, 7);
struct Card {
bool isDeck = false;
int num = -1;
};
auto flip = [&] () -> Card {
Card card;
if (dist_deck(rngesus) == 0) {
card.isDeck = true;
}
card.num = dist_card(rngesus);
return card;
};
vector<double> attempts;
int TOTAL_ATTEMPTS = 10000;
cout << "Enter the number of iterations to run: ";
cin >> TOTAL_ATTEMPTS;
for (int i = 0; i < TOTAL_ATTEMPTS; ++i) {
vector<bool> deck(8, false);
bool isDeckFinished = false;
int cards_made = 0;
while (!isDeckFinished) {
Card c = flip();
if (c.isDeck) {
deck[c.num] = true;
}
isDeckFinished = true;
for (bool hasCard : deck) {
isDeckFinished = isDeckFinished && hasCard;
}
cards_made++;
}
attempts.push_back(double(cards_made));
}
auto shrink_avg =
[] (vector<double> set, int chunk_size) -> vector<double> {
vector<double> out;
for (int i = 0; i < set.size() / chunk_size; i++) {
double total_i = 0;
for (int j = 0; j < chunk_size; j++) {
int index = i * chunk_size + j;
if (index < set.size()) {
total_i += set[index];
} else {
break;
}
}
double avg_i = double(total_i) / chunk_size;
out.push_back(avg_i);
}
return out;
};
vector<double> shrinker(attempts);
while (shrinker.size() > 1) {
shrinker = shrink_avg(shrinker, 10);
}
cout << "Average cards needed per deck: " << shrinker[0] << endl;
cout << "(" << TOTAL_ATTEMPTS << " iterations)" << endl;
int delay = 0; cin >> delay;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment