#include <iostream>
#include <random>
#include <cstdint>
#include <limits>
#include <limits.h> // for CHAR_BIT
template <class URBG = std::mt19937, class UIntType = std::uint32_t>
struct Coin {
bool toss() {
if (!bitCount) {
cache = std::uniform_int_distribution<UIntType>{
std::numeric_limits<UIntType>::min(),
std::numeric_limits<UIntType>::max()}(gen);
bitCount = sizeof(UIntType) * CHAR_BIT;
}
bool res = cache & 1;
cache >>= 1;
--bitCount;
return res;
}
URBG gen{std::random_device{}()};
UIntType cache = 0;
size_t bitCount = 0;
};
double sim(int total, int consec, int repeat) {
auto sim1 = [coin = Coin<>{}](int total, int consec) mutable {
for (int heads = 0; total--;)
if (coin.toss()) heads = 0; else if (++heads == consec) return 1;
return 0;
};
int cnt = 0;
for (int i = 0; i < repeat; ++i) cnt += sim1(total, consec);
return static_cast<double>(cnt) / repeat;
}
int main() {
std::cout << sim(100, 5, 10'000'000) * 100 << "%\n";
}