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 How to Generate Random Value by Dice Roll in C++? In C++, we can simulate a dice roll by generating random numbers within a specific range. In this article, we will learn how to generate random values by dice roll in C++. Example: Input: Roll a diceOutput:Dice roll result is: 4Generating Random Values Similar to a Dice RollTo simulate a dice roll, 2 min read Number Guessing Game in C++ using rand() Function In this article, we will develop a C++ game for guessing a secret number with three difficulty levels. In this game, the computer generates a secret number in the range of 1 to 100, and the player has to guess it.The game has three difficulty levels. A player's chances of guessing are limited by th 8 min read Generate a random Binary String of length N Given a positive integer N, the task is to generate a random binary string of length N. Examples: Input: N = 7Output: 1000001 Input: N = 5Output: 01001 Approach: The given problem can be solved by using the rand() function that generates a random number over the range [0, RAND_MAX] and with the help 5 min read Program to generate random alphabets Prerequisite : rand() and srand() Given all alphabets in a character array, print a string of random characters of given size.We will use rand() function to print random characters. It returns random integer values. This number is generated by an algorithm that returns a sequence of apparently non-r 4 min read How to Initialize a Vector with Values between a Range in C++? In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector 2 min read Like