c++ - Generate random numbers using C++11 random library -


as title suggests, trying figure out way of generating random numbers using new c++11 <random> library. have tried code:

std::default_random_engine generator; std::uniform_real_distribution<double> uniform_distance(1, 10.001); 

the problem code have every time compile , run it, generates same numbers. question other functions in random library can accomplish while being random?

for particular use case, trying range within [1, 10]

stephan t. lavavej (stl) microsoft did talk @ going native how use new c++11 random functions , why not use rand(). in it, included slide solves question. i've copied code slide below.

you can see full talk here: http://channel9.msdn.com/events/goingnative/2013/rand-considered-harmful

#include <random> #include <iostream>  int main() {     std::random_device rd;     std::mt19937 mt(rd());     std::uniform_real_distribution<double> dist(1.0, 10.0);      (int i=0; i<16; ++i)         std::cout << dist(mt) << "\n"; } 

we use random_device once seed random number generator named mt. random_device() slower mt19937, not need seeded because requests random data operating system (which source various locations, rdrand example).


looking @ this question / answer, appears uniform_real_distribution returns number in range [a, b), want [a, b]. that, our uniform_real_distibution should like:

std::uniform_real_distribution<double> dist(1, std::nextafter(10, dbl_max)); 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -