Last active
December 11, 2015 01:29
-
-
Save mlund/4523628 to your computer and use it in GitHub Desktop.
Random float in range [0:1[ using C++11's Mersenne Twister as well as a non-deterministic seed.
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> | |
#include <iostream> | |
template<typename T, typename Tengine=std::mt19937> | |
class RandOne { | |
std::uniform_real_distribution<T> dist_; | |
Tengine eng_; | |
public: | |
/* constructor w. non-deterministic seed */ | |
RandOne() : dist_(0,1) { | |
std::random_device rd; | |
eng_.seed(rd()); | |
} | |
/* random float [0:1[ */ | |
T operator()() { | |
return dist_(eng_); | |
} | |
}; | |
int main() { | |
RandOne<double> r; | |
std::cout << r(); // -> random double in range [0:1[ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment