How to Generate Random Double in C++

Use C++11
<random>
Library to Generate a Random Double in C++ 
Use
std::rand
Function to Generate a Random Double in C++
This article will explain several methods of how to generate random double
floatingpoint 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/pseudorandom number generation. Any application requiring highquality random numbers must use these methods, but other cases can also benefit from this clean and featurerich STL interface.
std::random_device
object, which is initialized at first, is a nondeterministic 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 pseudorandom 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 [10100]
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 singleprecision 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 highquality 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 pseudorandom integer between 0 and RAND_MAX
(both included). Since the RAND_MAX
value is implementationdependent, 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 doubleprecision floatingpoint 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