How to Generate Random Number in Range in C++? Last Updated : 23 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 a Range in C++To generate a random number in a range we have to make use of <random> library functionalities by following the below approach: Approach:Get a seed for the random number engine by creating a random_device rd object.Seed an mt19937 object with the rd. Now, create a uniform_int_distribution<> object. This models a random number generator that produces uniformly distributed integers within a specified range. Pass the minimum and maximum values of the range as parameters.Call the distributor by passing the generator as a parameter to it to get a generated random number in a range.C++ Program to Generate a Random Number in a Range The below program demonstrates how we can generate a random number in a given range in C++. C++ // C++ program to generate a random number in a range #include <iostream> #include <random> using namespace std; int main() { // Define range int min = 1; int max = 20; // Initialize a random number generator random_device rd; mt19937 gen(rd()); uniform_int_distribution<> distrib(min, max); // Generate random number in the range [min, max] int randomValue = distrib(gen); cout << "Random number between " << min << " and " << max << " is " << randomValue << endl; return 0; } OutputRandom number between 1 and 20 is 18 Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Generate Random Number in Range in C++? S sonijaiog3d Follow Improve Article Tags : C++ Programs C++ cpp-random CPP Examples Practice Tags : CPP Similar Reads 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 Create a Random Alpha-Numeric String in C++? Creating a random alpha-numeric string in C++ means generating random characters from the set of alphanumeric characters (i.e., âAâ-âZâ, âaâ-âzâ, and â0â-â9â) and appending them to a string. In this article, we will learn how to create a random alpha-numeric string in C++. Example Output:a8shg1laCre 2 min read C++ program to generate random number The below implementation is to generate random number from 1 to given limit. The below function produces behavior similar to srand() function. Prerequisite : random header in C++ | Set 1(Generators) CPP // C++ program to generate random number #include <bits/stdc++.h> using namespace std; // F 1 min read Generate Random Double Numbers in C++ 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 a 2 min read How to generate a vector with random values in C++? Generating a vector with random values means creating a vector of n elements and initialize each element some random values. In this article, we will learn different methods to initialize a vector with random values in C++.The recommended method to initialize a vector with random values is by using 3 min read Like