How to generate random numbers in C++
A random number is either chosen or selected arbitrarily. The random numbers can be used as keys to perform cryptography for secure communication or in hashing to add random values to passwords during hashing to make them strong.
To generate random numbers in C++, we can use the <cstdlib> library and <random> library. Let's discuss them in detail.
The <cstdlib> library
The <cstdlib> library provides two functions rand() and srand().
The rand() function is used to generate RAND_MAX that is a macro defined in the library with a variable value, the lowest being 32767.
The srand() function is used to seed the rand() function so that when we re-run the program, we get the same values by the rand() function after every run. The srand() function takes in the seed value as an argument. Conventionally, we provide the current system time to the function so that we always get a different sequence of random numbers during each run.
Code example
Below we can see the code that shows how the rand() and srand() functions are used:
#include <iostream>// for rand() and srand() functions#include <cstdlib>// for getting current system time#include <ctime>using namespace std;int main() {// provide system time as seed valuesrand(static_cast<unsigned int>(time(0)));// loop to display 5 valuesfor(int x = 0; x < 5; x++){// displaying a random value onto the consolecout << "Random Number " << x << " : " << rand() << endl;}return 0;}
Code explanation
Lines 2–5: We include the
<cstdlib>and<ctime>library for therand(),srand(), andtime()functions.Line 11: We provide the current system time using the
time()function to thesrand()function as an argument to generate random numbers after each run.Lines 14–17: We run a for loop from 0 to 5 to print five random values onto the console using the
rand()function in a print statement enclosed in the loop.
Note: To get same values on each run, comment out the
srand()function.
Using the random library
The <random> library provides various functions and classes to generate random numbers.
In the random library, we have a Mersene Twister engine class (mt19937) used for random number generation that can be initialized as follows:
mt19937 gen(rd())
Here, rd is an instance of the random_device class that is used to provide a random seed to the generator for initialization so that we get random numbers after each run.
We also have to provide the range of the randomly produced numbers using an object of the uniform_int_distribution class. Below we see how we can create the class so it generates numbers between 1 and 100.
uniform_int_distribution<> dist(1, 100);
To generate random numbers, we pass the generator object to the distribution class object as an argument as follows:
dist(gen)
Below, we can see a code example that uses the random library and its provided methods to generate random numbers.
#include <iostream>// to generate random numbers#include <random>using namespace std;int main() {// get seed value from hardwarerandom_device rd;// make an object of the engine classmt19937 gen(rd());// define the range of random numbersuniform_int_distribution<> dist(1, 100);// display 5 generated numbers onto the consolefor (int i = 0; i < 5; ++i) {cout << "Random number: " << dist(gen) << endl;}return 0;}
Below, we can see a brief explanation of the above code:
Line 9: We create an instance of the
random_deviceclass namedrdthat will be used as the seed value.Line 11: We create an instance of the
mt19937class namedgenthat will be used to generate random numbers, and we pass itrdas a constructor argument.Line 14: We make a
uniform_int_distributionclass object nameddistand pass 1 and 100 as constructor arguments to define the range of random numbers to be generated.Lines 16–19: We run a loop from 0 to 5 to print five random numbers using the
dist()function by passing the generator objectgenas an argument.
Free Resources