This post will discuss how to generate random numbers in C++.

1. Using std::rand function

The simplest and most common way to generate random numbers in C++ is to use the rand() and srand() functions. These functions are defined in the <cstdlib> header and are part of the C standard library. The rand() function returns a pseudo-random integer in the range [0, RAND_MAX], where RAND_MAX is a constant that is at least 32767.

The srand() function sets the seed value for the rand() function. If we don’t call srand() before calling rand(), the seed value is set as if we called srand(1). This means that every time we run our program, we will get the same sequence of random numbers from rand(). To avoid this, we should call srand() with a different seed value every time we run our program. A common way to do this is to use the current time as the seed value, like this:

Download  Run Code

 
The rand() and srand() functions are very easy to use and understand. However, they have some drawbacks that we should be aware of:

  • The rand() function generates a pseudo-random integer. The pseudo-random means that the numbers are not truly random, but are generated by a deterministic algorithm that depends on a seed value.
  • The rand() function can only generate integers up to RAND_MAX, which may not be enough for some applications. The rand() function also has a low precision, as it can only generate multiples of 1/RAND_MAX.
  • The rand() function uses a linear congruential generator (LCG) as its algorithm, which is known to have many flaws and weaknesses. The rand() function also has a low randomness, which means that it has patterns and correlations among the numbers. It also has a low unpredictability, which means that it is easy to guess or reverse-engineer the numbers.

 
Therefore, we should only use this function when we need a quick and simple way to generate random numbers, and when we don’t care much about the range, precision, quality, or security of the randomness.

2. Using the <random> header

A better option to generate random numbers in C++ is to use the <random> header. This header provides a modern and flexible way to generate random numbers with various features and options. The <random> header consists of four main components:

  • Engines: These are classes that implement different algorithms to generate pseudo-random numbers. For example, some of the engines are: minstd_rand (LCG), mt19937 (Mersenne Twister), ranlux48 (RANLUX), etc.
  • Distributions: These are classes that transform the output of engines into different ranges and shapes. For example, some of the distributions are: uniform_int_distribution (uniform integers), normal_distribution (normal or Gaussian), bernoulli_distribution (boolean), etc.
  • Seeds: These are values that initialize or reseed an engine. We can use any unsigned integer or an array of unsigned integers as a seed. We can also use a special class called random_device to generate non-deterministic seeds from a hardware source of randomness.
  • Variates: These are objects that combine an engine and a distribution to produce random numbers according to a specific algorithm and shape. We can create variates by passing an engine and a distribution as arguments to a constructor or a function call.

To use this function, we need to choose an engine and a distribution that suit our needs, create a variate with them, and call it to generate random numbers. For example, the following code generates random integers using the Mersenne Twister engine and the uniform integer distribution:

Download  Run Code

 
Instead of using std::random_device, we can use the Chrono library to obtain seed for the random number engine mersenne_twister_engine. For example:

Download  Run Code

 
The <random> header has some advantages over the rand() and srand() functions:

  • It has a larger range and precision. The <random> header can generate integers up to the maximum value of the unsigned int type, which is usually 4294967295.
  • It has a higher quality and security. The <random> header offers various engines that have better algorithms than the LCG used by rand().
  • It has more flexibility and versatility. The <random> header allows us to choose from different engines and distributions to generate random numbers with different shapes and properties.

3. Using Boost.Random library

A third option to generate random numbers in C++ is to use Boost.Random library, which is the part of Boost C++ Libraries collection. It provides similar functionality as the <random> header, but with more engines and distributions to choose from. Some of the engines are: knuth_b (Shuffle Order), lagged_fibonacci (Lagged Fibonacci), etc. Some of the distributions are: lognormal_distribution (log-normal), cauchy_distribution (Cauchy), etc. For example, the following code generates random integers between 1 and 10 using the Boost.Random library:

Download Code

That’s all about generating random numbers in C++.