Created
April 21, 2015 08:30
-
-
Save 607011/f8b34d468ecc4c2e044c to your computer and use it in GitHub Desktop.
Warming up and using the Mersenne-Twister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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