How to Generate Random Double in C++

  1. Use C++11 <random> Library to Generate a Random Double in C++
  2. Use std::rand Function to Generate a Random Double in C++

This article will explain several methods of how to generate random double floating-point numbers in C++.

Use C++11 <random> Library to Generate a Random Double in C++

Since the C++11 version, the standard library provides classes and methods for random/pseudo-random number generation. Any application requiring high-quality random numbers must use these methods, but other cases can also benefit from this clean and feature-rich STL interface.

std::random_device object, which is initialized at first, is a non-deterministic uniform random bit generator used to seed the random number engine std::default_random_engine initialized on the next line. This step ensures the engine does not produce the same sequences. Several random number engines are implemented in the C++ numerics library, and they differ in terms of time/space requirements (see the full list here).

In the following example, we use std::default_random_engine to generate pseudo-random values, but you can initialize the specific algorithm engine as required by your application constraints. Next, we initialize a uniform distribution and pass min/max values as optional arguments. Finally, we print 5 random double values from [10-100] interval to the console.

#include <iostream>
#include <random>
#include <iomanip>

using std::cout;
using std::endl;
using std::setprecision;

// Modify as needed
constexpr int MIN = 10;
constexpr int MAX = 100;

int main()
{
    std::random_device rd;
    std::default_random_engine eng(rd());
    std::uniform_real_distribution<double> distr(MIN, MAX);

    for (int n = 0; n < 5; ++n) {
        cout << setprecision(10)
             << distr(eng) << "\n";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

73.68930968
24.37712986
40.37815433
77.24899374
94.62192505

Note that std::uniform_real_distribution< T > yields undefined behavior if one of the following types is not passed as template parameter: float, double, or long double. The following example generates single-precision floating point numbers:

#include <iostream>
#include <random>
#include <iomanip>

using std::cout;
using std::endl;
using std::setprecision;

// Modify as needed
constexpr int MIN = 10;
constexpr int MAX = 100;

int main()
{
    std::random_device rd;
    std::default_random_engine eng(rd());
    std::uniform_real_distribution<float> distr(MIN, MAX);

    for (int n = 0; n < 5; ++n) {
        cout << setprecision(10)
             << distr(eng) << "\n";
    }
    cout << endl;

    return EXIT_SUCCESS;
}

Use std::rand Function to Generate a Random Double in C++

rand function is part of the C standard library random number generation facilities. It is not recommended for applications requiring high-quality randomness, but can be utilized for multiple scenarios e.g. if one wants to fill matrices or vectors with random numbers.

This function generates a pseudo-random integer between ​0​ and RAND_MAX (both included). Since the RAND_MAX value is implementation-dependent, and the guaranteed minimum value is only 32767, generated numbers have constrained randomness. This function should be seeded with std::srand (preferably passing the current time using std::time). Finally, we can generate double-precision floating-point values with some cumbersome arithmetic.

#include <iostream>
#include <random>
#include <iomanip>

using std::cout;
using std::endl;
using std::setprecision;

// Modify as needed
constexpr int MIN = 10;
constexpr int MAX = 100;

int main()
{
    std::srand(std::time(nullptr));
    for (int i = 0; i < 5; i++)
        cout << setprecision(10)
             << MIN + (double)(rand()) / ((double)(RAND_MAX/(MAX - MIN)))
             << endl;
    return EXIT_SUCCESS;
}

Output:

84.70076228
11.08804226
20.78055909
74.35545741
18.64741151

Related Article - C++ Double

  • How to Round a Double to an Int in C++