/******************************************************************************
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()
{
constexpr size_t num_boxes = 2;
constexpr size_t num_balls = 1000;
enum Ball {silver = 0, gold =1};
using Box = Ball[num_balls];
Box boxes[num_boxes]{};//all silver;
for(Ball& ball : boxes[0])
ball = gold;
boxes[1][0]=gold;
auto random_range = [](int a, int b){ return std::uniform_int_distribution<>{a,b}(get_random_gen_ref()); };
size_t possible_outcomes=0;
size_t event=0;
for(size_t i = 0; i<100000000; ++i){
Box & box = boxes[random_range(0,num_boxes-1)];
auto ballindex = random_range(0,num_balls-1);
if(box[ballindex]==gold){
++possible_outcomes;
auto secondballindex = random_range(0,num_balls-2);
if(secondballindex==ballindex)
secondballindex = num_balls-1;
if(box[secondballindex]==gold)
++event;
}
}
std::cout<<"Events: "<<event<<"\nOut of: "<<possible_outcomes
<<"\nRaito: " << static_cast<double>(event)/possible_outcomes <<std::endl;
}