Creating random numbers using a random C ++ 11 library

As the name suggests, I'm trying to find a way to generate random numbers using the new C ++ 11 <random> library. I tried this with this code:

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

The problem with the code that I have is that every time I compile and run it, it always generates the same numbers. So my question is: what other functions in a random library can accomplish this while being truly random?

For my specific use case, I tried to get a value in the range [1, 10]

+120
c ++ random c ++ 11 range
Oct 29 '13 at 18:01
source share
5 answers

Stefan T. Lavavey (stl) from Microsoft made a presentation at Going Native on how to use the new random functions in C ++ 11 and why not use rand() . He included a slide in it that basically solves your question. I copied the code from this slide below.

You can see his full report 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); for (int i=0; i<16; ++i) std::cout << dist(mt) << "\n"; } 

We use random_device once for the random_device random number generator named mt . random_device() slower than mt19937 , but mt19937 does not need mt19937 because it requests random data from your operating system (which will receive data from different places, for example, RdRand ).




Looking at this question / answer , it seems that uniform_real_distribution returns a number in the range [a, b) where you want [a, b] . To do this, uniform_real_distibution should look like this:

 std::uniform_real_distribution<double> dist(1, std::nextafter(10, DBL_MAX)); 
+168
Oct 29 '13 at 18:48
source share

My “random” library provides a convenient wrapper around random C ++ 11 classes. You can do almost anything with a simple get method.

Examples:

  1. Random number in range

     auto val = Random::get(-10, 10); // Integer auto val = Random::get(10.f, -10.f); // Float point 
  2. Random boolean

     auto val = Random::get<bool>( ) // 50% to generate true auto val = Random::get<bool>( 0.7 ) // 70% to generate true 
  3. Random value from std :: initilizer_list

     auto val = Random::get( { 1, 3, 5, 7, 9 } ); // val = 1 or 3 or... 
  4. Random iterator from iterator range or entire container

     auto it = Random::get( vec.begin(), vec.end() ); // it = random iterator auto it = Random::get( vec ); // return random iterator 

And even more things! Check out the GitHub page:

https://github.com/effolkronium/random

+19
Aug 05 '17 at 18:42 on
source share

Here is something that I wrote only on these lines:

 #include <random> #include <chrono> #include <thread> using namespace std; //============================================================== // RANDOM BACKOFF TIME //============================================================== class backoff_time_t { public: random_device rd; mt19937 mt; uniform_real_distribution<double> dist; backoff_time_t() : rd{}, mt{rd()}, dist{0.5, 1.5} {} double rand() { return dist(mt); } }; thread_local backoff_time_t backoff_time; int main(int argc, char** argv) { double x1 = backoff_time.rand(); double x2 = backoff_time.rand(); double x3 = backoff_time.rand(); double x4 = backoff_time.rand(); return 0; } 

~

+2
Dec 21 '16 at 2:44
source share

Here is some resource you can read about the pseudo random number generator.

https://en.wikipedia.org/wiki/Pseudorandom_number_generator

Typically, random numbers in a computer require a seed (this number may be the current system time).

replace

 std::default_random_engine generator; 

From

 std::default_random_engine generator(<some seed number>); 
0
May 12 '19 at 11:47
source share

You have two common situations. Firstly, you want to get random numbers and not fuss about quality or speed of execution too much. In this case, use the following macro

 #define uniform() (rand()/(RAND_MAX + 1.0)) 

which gives you a p value in the range from 0 to 1 - epsilon (if only RAND_MAX is greater than the precision of the double, but worry about it when you come to it).

int x = (int) (uniform () * N);

Now gives a random integer from 0 to N -1.

If you need other distributions, you need to convert p. Or sometimes it's easier to call uniform () several times.

If you want repeatable behavior, seed with constant, otherwise seed with time () call.

Now, if you are concerned about the quality or lead time, rewrite the form (). But for the rest, do not touch the code. Always keep uniform () at 0-1 minus epsilon. Now you can wrap the C ++ random number library to create a better form (), but this is a kind of mid-range option. If you are concerned about the characteristics of RNGs, it is also worth spending a little time to understand how the underlying methods work and then provide them. Thus, you have full control over the code, and you can guarantee that with the same seed the sequence will always be exactly the same, regardless of the platform or C ++ version that you are referring to.

-2
Oct 10 '16 at 12:37
source share



All Articles