Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to generate random numbers in C++?

What is the best way to generate random numbers?

like image 449
Hamed Avatar asked Feb 27 '12 20:02

Hamed


People also ask

Does C have a random number generator?

C library function - rand()The C library function int rand(void) returns a pseudo-random number in the range of 0 to RAND_MAX. RAND_MAX is a constant whose default value may vary between implementations but it is granted to be at least 32767.

Which random number generator is the best C++?

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.

What is difference between rand () and Srand ()?

The rand() function in C++ is used to generate random numbers; it will generate the same number every time we run the program. In order to seed the rand() function, srand(unsigned int seed) is used. The srand() function sets the initial point for generating the pseudo-random numbers.


2 Answers

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 occurences[8] = {0};

srand(time(0));
for (int i = 0; i < 100000; ++i)
    ++occurences[irand(1,7)];

for (int i = 1; i <= 7; ++i)
    printf("%d ", occurences[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.

like image 27
LihO Avatar answered Sep 21 '22 13:09

LihO


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).

like image 62
Kerrek SB Avatar answered Sep 21 '22 13:09

Kerrek SB