Generate Random Double Numbers in C++ Last Updated : 16 Feb, 2023 Comments Improve Suggest changes Like Article Like Report Double is a data type just like a float but double has 2x more precision than float. One bit for the sign, 11 bits for the exponent and 52* bits for the value constitute the 64-bit IEEE 754 double precision Floating Point Number known as "double." Double has 15 decimal digits of precision. In this article, we generate random double numbers in C++. Methods to generate random double The random double number can be generated using a few methods which are mentioned below: Using random() functionUsing uniform_real_distribution and default_random_engine. 1. Using random() function Using the random function is the method where we get random integers and by using them we create a random double number by performing certain operations in it. You can set lower and upper bounds in it. random(): It generate a random integer. Example: C++ // C++ program to generate random double // Using random function #include <iostream> #include <time.h> using namespace std; // Driver Code int main() { const long max_rand = 1000000L; double lower_bound = 0; double upper_bound = 100; srandom(time(NULL)); // Using random function to // get random double value double random_double = lower_bound + (upper_bound - lower_bound) * (random() % max_rand) / max_rand; cout << random_double << endl; return 0; } Output35.99522. Using uniform_real_distribution and default_random_engine When we want to find random numbers after defining the upper and lower bound then we can use this method. uniform_real_distribution: The random library now includes the uniform real distribution class, whose member functions generate random real numbers or continuous values with uniform probability from a specified input range. default_random_engine: This class of pseudo-random number generators produces random numbers. min(): It gives back the lowest value specified by the operator ().max(): It gives back the highest value specified by the operator ().operator(): A fresh random number is given back. Example: C++ // C++ program to generate random double // uniform_real_distribution and // default_random_engine #include <iostream> #include <random> using namespace std; // Driver Code int main() { // Declaring the upper and lower // bounds double lower_bound = 0; double upper_bound = 100; uniform_real_distribution<double> unif(lower_bound, upper_bound); default_random_engine re; // Getting a random double value double random_double = unif(re); cout << random_double << endl; return 0; } Output13.1538 Time complexity: O(1). Auxiliary Space: O(1). Comment More infoAdvertise with us Next Article Generate Random Double Numbers in C++ A akashjha2671 Follow Improve Article Tags : Technical Scripter C++ Programs C++ Technical Scripter 2022 Practice Tags : CPP Similar Reads How to Generate Random Number in Range in C++? In C++, we have a <random> header that consists of standard library facilities for random number generation. In this article, we will learn how to generate a random number in range in C++. Example: Input: Range: 1 to 20Output: Random number between 1 and 20 is: 18Generating a Random Number in 2 min read How to Seed a Random Number Generator in C++? In C++, seeding a random number generator is important for generating different sequences of random numbers on each program run. This process consists of initializing the generator with a starting value, known as a seed. This ensures the randomness and unpredictability required for various applicati 2 min read How to Handle Large Numbers in C++? In C++, when working with very large numbers the standard data types like int can become insufficient due to their limited range. In this article, we will learn how we can handle large numbers in C++. Handle Large Numbers in C++To store integers of different sizes, C++ offers built-in data types lik 3 min read Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two 3 min read How to Round a Double to an int in C++? In C++, the double data type is used to store floating-point numbers with double precision. In this article, we will learn how to round a double value to an int in C++. Example: Input: double val= 2.6 Output: Integer for double value 2.5 is: 3 Rounding a Double to an Int in C++To round a double valu 2 min read Like