In this guide, we will explore the process of creating random double precision floating-point numbers in C++.
In C++, the standard library provides a variety of functions for generating random double numbers. Within this library, you can find the std::random_device class, responsible for generating seeds, and the std::mt19937 class, a widely used Mersenne Twister pseudo-random number generator. The Mersenne Twister algorithm is highly regarded for its long cycle length and high-quality random number generation.
Recognize that the seemingly random numbers are not truly random since they are generated using deterministic algorithms. In various practical scenarios, numerical sequences resembling genuine randomness can be achieved by employing reliable pseudo-random number generators and proper seeding techniques. To prevent the occurrence of repetitive patterns in the random number sequences, it is crucial to ensure accurate seeding procedures are in place.
Methods that generate random double
There exist multiple methods to generate a random decimal number, a few of which are outlined as follows:
- Employing the random function
- Leveraging uniformrealdistribution along with defaultrandomengine.
Using random function
Obtaining random numbers within a specified range, usually between 0 and RAND_MAX, is achievable in C++ through the utilization of the random function. By applying scaling and shifting transformations to these integers, random double precision numbers can be produced. To define the range of double precision floating-point numbers, it is necessary to establish both lower and upper boundaries. To guarantee that the generated double values adhere to the specified constraints, the random integers are scaled accordingly to fit within this defined range.
Example:
#include <iostream>
#include <time.h>
using namespace std;
int main()
{
const long max_limit = 1000000L;
double lower_limit = 0;
double upper_limit = 1000;
srandom(time(NULL));
// Utilising the random function to generate random double number.
double random_double = lower_limit+ (upper_limit - lower_limit) *
(random() % max_limit) / max_limit;
cout << "The random double number is: "<<random_double << endl;
return 0;
}
Output:
The random double number is: 512.058
The random double variable is defined with a specified range between a lowerlimit of 0 and an upperlimit of 1000. To ensure unique sequences of random numbers for each program run, the srandom function initializes the random number generator based on the current time. Within the main function, the random function produces a pseudo-random integer within the range of 0 to maxlimit (1000000), which is then adjusted and proportioned to fit within the specified double range. After this adjustment, the integer is transformed into a double-precision floating-point number and stored in the randomdouble variable. Finally, a relevant message along with the generated random double number is displayed on the console.
Using uniform_real_distribution and default_random_engine
This approach may be used to find random numbers once the upper and lower bounds have been defined.
- uniformrealdistribution: A continuous uniform distribution of real numbers within a certain range is represented by this class template. The lower and upper limits of the distribution are the two parameters it requires. In combination with a random number engine such as defaultrandomengine, it generates evenly distributed random double-precision floating-point numbers at random within the given bounds.
- defaultrandomengine: This class template generates a series of random values by serving as a random number engine. Usually, it uses the Mersenne Twister algorithm, which is a popular and high-quality pseudo-random number generator. A seed value is used to initialise instances of defaultrandomengine, which determines the starting point of the random number sequence.
- min: Returns the lowest value that the operator specifies.
- max: Returns the maximum value that the operator has specified.
- operator: Returns a new random number.
Example:
#include <iostream>
#include <random>
int main()
{
// Defining the range for random double numbers.
double lower_limit = 0.0;
double upper_limit = 1000.0;
//Start the random number generator.
std::random_device rd;
std::default_random_engine gen(rd());
std::uniform_real_distribution<double> dis(lower_limit, upper_limit);
// Generating a random double number within the given range.
double random_double = dis(gen);
// Printing the generated random double number.
std::cout << "The random double number between the " << lower_limit << " and " << upper_limit << " is" << ": " << random_double << std::endl;
return 0;
}
Output:
The random double number between the 0 and 1000 is: 375.641
Explanation:
In this instance, the random double value is determined within the range defined by setting the lower limit to 0.0 and the upper limit to 1000.0. The std::randomdevice serves the purpose of producing a seed for the random number generator, while std::defaultrandomengine is employed as the random number engine upon initialization. Defining the range for the random double values involves the creation of a std::uniformrealdistribution. By utilizing both the distribution and engine, a random double value is generated and then assigned to the randomdouble variable.
Finally, a suitable message detailing the range is displayed on the console alongside the randomly generated decimal number.