Generate random float value in C++
This post will discuss how to generate random float values in C++.
1. Using std::uniform_real_distribution
Since C++11, we can produce uniformly distributed floating-point values between two numbers with std::uniform_real_distribution. Consider the following code, which produces high-quality floating-point random values in the closed interval [1, 10] using std::uniform_real_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 |
#include <iostream> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dist(1, 10); for (int i = 0; i < 100; i++) { std::cout << dist(gen) << std::endl; } return 0; } |
Note that there are numerous other predefined random number generators defined in the header <random>, as listed here. Before C++11, you may use Mersenne Twister by boost library to generate random float values. For example, the following solution uses Boost.Random with boost::random::uniform_real_distribution and std::mt19937 algorithm to generate a random floating-point value in closed range [1, 10].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <boost/random/mersenne_twister.hpp> #include <boost/random/uniform_real_distribution.hpp> int main() { boost::random::mt19937 gen; boost::random::uniform_real_distribution<> dist(1, 10); for (int i = 0; i < 100; i++) { std::cout << dist(gen) << std::endl; } return 0; } |
2. Using rand() function
Another simple, but less preferred solution to generate pseudo-random numbers in C++ is with rand() function. It returns a random number between 0 and RAND_MAX, and can be used to generate floating-point random values in any arbitrary closed interval. Don’t forget to seed the random number generator with srand() before invoking rand(). Both rand() and srand() functions are defined in <cstdlib> header.
For example, the following will generate a floating-point number in close range [0, 1].
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <random> #include <ctime> int main() { srand(time(NULL)); for (int i = 0; i < 100; i++) { float r = rand() / static_cast<float>(RAND_MAX); std::cout << r << std::endl; } return 0; } |
We can easily extend the solution to generate an arbitrary float value between two specified values. For example, the expression low + rand() * (high - low) / RAND_MAX; generates a random floating-point value in the closed range [low, high], as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <random> #include <ctime> int main() { int low = 1; int high = 10; srand(time(NULL)); for (int i = 0; i < 100; i++) { float r = low + static_cast<float>(rand()) * static_cast<float>(high - low) / RAND_MAX; std::cout << r << std::endl; } return 0; } |
That’s all about generating random float values 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 :)