Skip to content

Instantly share code, notes, and snippets.

@mlund
Last active December 11, 2015 01:29
Show Gist options
  • Save mlund/4523628 to your computer and use it in GitHub Desktop.
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.
#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