What is the best way to generate random numbers?
-
2Why is `rand` not good to use? – David Schwartz Feb 27 '12 at 20:16
-
2because you should divide and plus and ... to make the number between two numbers! there must be beter functions – Hamed Feb 27 '12 at 20:27
-
4@DavidSchwartz I came here because rand is generating the same results every time... – Tomáš Zato Nov 23 '14 at 13:32
-
1@TomášZato So show the code that had the problem and ask how to fix it. – David Schwartz Nov 24 '14 at 02:07
-
@DavidSchwartz "Why is `rand` not good to use?" - See [rand() Considered Harmful](https://learn.microsoft.com/en-us/events/goingnative-2013/rand-considered-harmful). – Jesper Juhl Aug 10 '23 at 07:54
5 Answers
You should use <random>
:
#include <random>
typedef std::mt19937 rng_type;
std::uniform_int_distribution<rng_type::result_type> udist(0, 7);
rng_type rng;
int main()
{
// seed rng first:
rng_type::result_type const seedval = get_seed(); // get this from somewhere
rng.seed(seedval);
rng_type::result_type random_number = udist(rng);
return random_number;
}
Pre C++11 you could find this either in TR1 (<tr1/random>
, std::tr1::mt19937
etc.), or in Boost.random, with essentially the same interface (though there are minor differences).

- 464,522
- 92
- 875
- 1,084
-
I don't think there needs to be a correlation between the distribution's return type and the engine's. I usually like to bind the distribution and the engine together unless I have a special reason for needing them separate. Also engines can take the seed in a constructor, so you can put it all in a single line: `auto rand = bind(uniform_int_distribution<>(0,7),mt19937(get_seed()));` – bames53 Feb 27 '12 at 20:37
-
@bames53: You're right, the integral types of the distribution and of the engine may well be different. Personally, I don't like gratuitous `bind`s very much, but sure, if you want a more compact form, go ahead. – Kerrek SB Feb 27 '12 at 20:41
-
1
-
1@TomášZato: You can use `std::random_device`. The quality of the result depends on your implementation, but it's a start. – Kerrek SB Nov 23 '14 at 15:26
If and only if:
you are not looking for "perfect uniformity" or
you have no C++11 support and not even TR1 (thus you don't have another choice)
then you might consider using the following C-style solution, which (for the sake of the reputation of this community ~ see rand() Considered Harmful) is written in strike-through font:
Here's the simple C-style function that generates random number from the interval from min
to max
, inclusive. Those numbers seem to be very close to being uniformly distributed.
int irand(int min, int max) {
return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
}
and don't forget to call srand
before you use it:
int occurrences[8] = {0};
srand(time(0));
for (int i = 0; i < 100000; ++i)
++occurrences[irand(1,7)];
for (int i = 1; i <= 7; ++i)
printf("%d ", occurrences[i]);
output: 14253 14481 14210 14029 14289 14503 14235
Also have a look at:
Generate a random number within range?
Generate random numbers uniformly over an entire range
and find some time and watch at least first 11 minutes of aforementioned video
Otherwise:
use <random>
just like it was pointed out by Kerrek SB already.

- 19,179
- 10
- 84
- 156

- 41,190
- 11
- 99
- 167
-
1Your `irand` implementation does *not* produce uniformly distributed random numbers: some values are more likely than others as explained in the [rand() considered harmful](http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful) talk. Your very example is described at the 7:30 mark. – syam Oct 08 '13 at 12:49
-
1@syam: I've seen a link to this speech few times already, but haven't paid attention to it. Finally I watched it and I've edited my answer appropriately :) – LihO Oct 08 '13 at 22:02
-
1To be fair, the speaker considers your implementation only "subtly" non-uniform, as opposed to other "hilariously" non-uniform solutions. I for one made much worse errors with `rand()`... ;) – syam Oct 09 '13 at 00:38
-
1@syam: But in case `
` is available, why not go for it? It's cleaner and better solution. It can be wrapped into a standalone helper function like my `irand` as well. – LihO Oct 09 '13 at 06:21 -
1Don't get me wrong, I wasn't advocating the use of your implementation over `
`, just pointing out that there are much worse mistakes to make. Of course ` – syam Oct 09 '13 at 12:53` should be preferred if it is available, however if you *must* make do with `rand()` then your solution is probably (one of?) the best we can get. -
`rand()` is great for my unit tests. But on Unix systems you can at least load bytes from `/dev/random` or `/dev/urandom`. Also the OpenSSL library offers RAND (try `man RAND` and `man RAND_bytes`). – Alexis Wilke Oct 28 '22 at 00:32
Boost.Random is an excellent library for producing pseudorandom numbers (or truly random, if the platform supports it).

- 3,320
- 15
- 20
-
5I checked their docs just to make sure they are not claiming to produce truly random numbers, and they are not. So why are you including that in your answer? – Kevin Crowell Feb 27 '12 at 20:19
-
1@KevinCrowell [Non-deterministic Uniform Random Number Generator](http://www.boost.org/doc/libs/1_49_0/doc/html/boost_random/reference.html#boost_random.reference.concepts.non_deterministic_uniform_random_number_generator) – spencercw Feb 27 '12 at 20:22
-
2It includes `random_device` (as does the now-standard
header) to provide access to a non-deterministic uniform random number generator on platforms that have such a device. (E.g. The OS collects random data from doing things like observing network traffic and provides access to that data via /dev/random) – bames53 Feb 27 '12 at 20:28
If you're talking standard C++ library pre C++11, rand and srand are your random number generators. There are ways to get more accuracy out of these functions than using modulus with integer arithmetic. You could use doubles, for example, if high speed isn't a concern and round the results to int.
As for custom libraries, if you really want good random distribution and speed, google Mersenne Twister. There are also options in boost.
With C++11 you have <random>
. http://en.cppreference.com/w/cpp/numeric/random

- 6,737
- 28
- 27
My 'random' library provide a high convenient wrapper around C++11 random classes. You can do almost all things with a simple 'get' method.
Examples:
- Random number in a range
auto val = Random::get(-10, 10); // Integer
auto val = Random::get(10.f, -10.f); // Float point
- Random boolean
auto val = Random::get<bool>( ) // 0.5% to generate true
auto val = Random::get<bool>( 0.7 ) // 0.7% to generate true
- Random value from a std::initilizer_list
auto val = Random::get( { 1, 3, 5, 7, 9 } ); // val = 1 or 3 or...
- Random iterator from iterator range or all 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:

- 523
- 4
- 10
-
Quite complete library you have there! I like the fact that one can load/save the current state since that way you do not need to reseed each time which makes it safer, I think. – Alexis Wilke Oct 29 '22 at 15:24