/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <random>
#include <thread>
#include <chrono>
#include <stdint.h>
std::mt19937_64& get_random_gen_ref() {
thread_local static std::random_device rd;
thread_local static size_t thread_id = std::hash<std::thread::id>{}(std::this_thread::get_id());
thread_local static long long time = std::chrono::high_resolution_clock::now().time_since_epoch().count();
thread_local static std::seed_seq seeds{
static_cast<uint_least32_t>(time>>32), static_cast<uint_least32_t>(time), //back up if rd() isn't truly random
static_cast<uint_least32_t>(thread_id >> 32), static_cast<uint_least32_t>(thread_id), //make sure it has a different seed than other threads
rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), //partially fill mt's state with random data
rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(),
rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(),
rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()
};
thread_local static std::mt19937_64 mt{seeds};
return mt;
}
int main()
{
enum Ball {silver = 0, gold =1};
using Box = Ball[2];
Box boxes[3] { {silver, silver}, {silver, gold}, {gold, gold} };
auto random_range = [](int a, int b){ return std::uniform_int_distribution<>{a,b}(get_random_gen_ref()); };
int possible_outcomes=0;
int event=0;
for(int i = 0; i<10000000; ++i){
Box & box = boxes[random_range(0,2)];
auto ballindex = random_range(0,1);
if(box[ballindex]==gold){
++possible_outcomes;
if(box[1-ballindex]==gold)
++event;
}
}
std::cout<<"Events: "<<event<<"\nOut of: "<<possible_outcomes
<<"\nRaito: " << static_cast<double>(event)/possible_outcomes <<std::endl;
}