Skip to content

Instantly share code, notes, and snippets.

@607011
Created April 21, 2015 08:30
Show Gist options
  • Save 607011/f8b34d468ecc4c2e044c to your computer and use it in GitHub Desktop.
Save 607011/f8b34d468ecc4c2e044c to your computer and use it in GitHub Desktop.
Warming up and using the Mersenne-Twister
#include <string>
#include "mt.h"
warmupRNG();
std::uniform_int_distribution<int> rollTheDice(1, 6);
for (int i = 0; i < 1000; ++i)
std::cout << rollTheDice(gRNG()) << ", ";
std::cout << std::flush;
#include "mt.h"
#include <array>
std::mt19937 &gRNG() {
static std::mt19937 *rng = new std::mt19937;
return *rng;
}
void warmupRNG(void)
{
std::array<int, std::mt19937::state_size> seed_data;
std::random_device r;
std::generate_n(seed_data.data(), seed_data.size(), std::ref(r));
std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
gRNG().seed(seq);
}
#include <random>
std::mt19937& gRNG();
extern void warmupRNG(void);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment