Generate random numbers in the specified range in C++
This post will discuss how to generate random numbers in the specified range in C++.
1. Using std::uniform_int_distribution
A simple solution to produce random numbers between two values (inclusive) in modern C++ is using std::uniform_int_distribution, which generates random integer values uniformly distributed on a specified closed interval.
The following solution produces high-quality integer random numbers in the given closed interval with std::uniform_int_distribution. It uses std::random_device to obtain seed for the standard mersenne_twister_engine std::mt19937, based on the Mersenne Twister algorithm.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <random> std::random_device rd; std::mt19937 gen(rd()); int random(int low, int high) { std::uniform_int_distribution<> dist(low, high); return dist(gen); } int main() { for (int i = 0; i < 10; i++) { std::cout << random(1, 100) << std::endl; } return 0; } |
The above solution is short and elegant, but it is available only on C++11 compliant compilers. Before C++11, you can use Mersenne Twister by boost library.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_int.hpp> #include <boost/random/variate_generator.hpp> boost::mt19937 engine; int random(int low, int high) { boost::uniform_int<> dist(low, high); boost::variate_generator<boost::mt19937&, boost::uniform_int<>> vgen(engine, dist); return vgen(); } int main() { for (int i = 0; i < 10; i++) { std::cout << random(1, 100) << std::endl; } return 0; } |
There are several other predefined random number generators in header <random>, as listed here. The following solution uses std::default_random_engine random number generator with std::uniform_int_distribution.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <random> std::default_random_engine gen; int random(int low, int high) { std::uniform_int_distribution<> dist(low, high); return dist(gen); } int main() { for (int i = 0; i < 10; i++) { std::cout << random(1, 100) << std::endl; } return 0; } |
Consider seeding the std::default_random_engine random number generator with the current time.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> #include <random> #include <chrono> unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); std::default_random_engine gen(seed); int random(int low, int high) { std::uniform_int_distribution<> dist(low, high); return dist(gen); } int main() { for (int i = 0; i < 10; i++) { std::cout << random(1, 100) << std::endl; } return 0; } |
2. Using rand() function
Another common, but less preferred way to generate random numbers in the specified range is using the rand() function. It is defined in the header <cstdlib> and returns a random value between 0 and RAND_MAX (both inclusive). To generate a random value in the closed range [low, high], you can use the expression low + rand() % (high - low + 1), after seeding the rand() function with a random value (say current time).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <ctime> #include <random> int random(int low, int high) { return low + rand() % (high - low + 1); } int main() { srand(time(NULL)); for (int i = 0; i < 10; i++) { std::cout << random(1, 100) << std::endl; } return 0; } |
3. Using std::experimental::randint
Although experimental, std::experimental::randint generates a random integer in the specified closed interval. It is defined in <experimental/random> header.
|
1 2 3 4 5 6 7 8 9 10 11 |
#include <iostream> #include <experimental/random> int main() { for (int i = 0; i < 10; i++) { std::cout << std::experimental::randint(1, 100) << std::endl; } return 0; } |
That’s all about generating random numbers in the specified range in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)