The <random> library in C++ provides pseudorandom number generation.
To compile and run the program:
Some of the generated numbers may be different when you run the sample.
The <random> library in C++ provides a wide range of random number generators and distributions. You can check the C++ reference for more information on other random quantities that C++ can provide.
In this example:
We use std::random_device to seed our random number generator.
std::mt19937 is a Mersenne Twister generator, which is a high-quality random number generator.
std::uniform_int_distribution and std::uniform_real_distribution are used to generate random integers and floating-point numbers respectively.
We demonstrate how to generate numbers in specific ranges.
We also show how to use a fixed seed for reproducible random number sequences.
Remember that the C++ random number facilities are more type-safe and generally of higher quality than the older C-style rand() function.