Skip to content

Instantly share code, notes, and snippets.

@griwes
Created May 17, 2013 15:43
Show Gist options
  • Save griwes/5599926 to your computer and use it in GitHub Desktop.
Save griwes/5599926 to your computer and use it in GitHub Desktop.
griwes@Griwes-Linux ~/tests $ clang++ main.cpp -std=c++11 -stdlib=libc++ -lc++abi
griwes@Griwes-Linux ~/tests $ ./a.out
Attempts: 1000000000
Successes: 499975732
S/A: 0.499976
Attempts with change: 499990979
Successes with change: 333311610
S/A (changed): 0.666635
Attempts without change: 500009021
Successes without change: 166664122
S/A (no change): 0.333322
griwes@Griwes-Linux ~/tests $ cat main.cpp
#include <iostream>
#include <random>
int main()
{
uint64_t attempts = 0;
uint64_t attempts_changed = 0;
uint64_t successes = 0;
uint64_t successes_changed = 0;
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> of_three(1, 3);
std::uniform_int_distribution<uint64_t> of_two(0, 1);
for (; attempts < 1000000000; ++attempts)
{
uint64_t winning = of_three(gen);
uint64_t selected = of_three(gen);
bool change = of_two(gen);
if (change)
{
++attempts_changed;
}
if (winning == selected)
{
if (!change)
{
++successes;
}
}
else if (change)
{
if (winning != selected)
{
++successes;
++successes_changed;
}
}
}
std::cout << "Attempts: " << attempts << std::endl;
std::cout << "Successes: " << successes << std::endl;
std::cout << "S/A: " << (double)successes / attempts << std::endl;
std::cout << "Attempts with change: " << attempts_changed << std::endl;
std::cout << "Successes with change: " << successes_changed << std::endl;
std::cout << "S/A (changed): " << (double)successes_changed / attempts_changed << std::endl;
std::cout << "Attempts without change: " << attempts - attempts_changed << std::endl;
std::cout << "Successes without change: " << successes - successes_changed << std::endl;
std::cout << "S/A (no change): " << (double)(successes - successes_changed) / (attempts - attempts_changed) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment