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.
Malcolm McLean Oct 10 '16 at 12:37 2016-10-10 12:37
source share