Random Number Generator in C++

To generate a random number in C++, you can use the library introduced in C++11, which provides a flexible set of features for random number generation. Here's an example of generating a random integer between 1 and 10, inclusive:


                 
                #include <iostream>
                #include <random>

                int main() {
                    // Random number generator
                    std::random_device rd;  // Obtain a random number from hardware
                    std::mt19937 gen(rd()); // Seed the generator
                    std::uniform_int_distribution<> distr(1, 10); // Define the range

                    // Generate and print a random integer between 1 and 10
                    std::cout << "Random Number: " << distr(gen) << std::endl;

                    return 0;
                }
 
                               
                

In this code:

This approach is more flexible and preferable for modern C++ applications compared to older methods like rand().

Preset number shortcuts between

Digit shortcuts

Code samples