C++ Program to Generate Random Number
<random> library to generate random numbers by creating a std::mt19937 engine and a distribution like std::uniform_int_distribution, then call the distribution with the engine to get a random number.Examples
How to Think About It
Algorithm
Code
#include <iostream> #include <random> int main() { std::random_device rd; // Seed generator std::mt19937 gen(rd()); // Random engine std::uniform_int_distribution<> dist(1, 100); // Range 1 to 100 int random_number = dist(gen); // Generate number std::cout << "Random number between 1 and 100: " << random_number << std::endl; return 0; }
Dry Run
Let's trace generating a random number between 1 and 100 through the code.
Seed creation
random_device rd creates a seed value, e.g., 123456789
Engine initialization
mt19937 gen is initialized with seed 123456789
Distribution setup
uniform_int_distribution dist is set for range 1 to 100
Generate random number
dist(gen) produces a number, e.g., 57
Output
Prints: Random number between 1 and 100: 57
| Step | Action | Value |
|---|---|---|
| 1 | Seed from random_device | 123456789 |
| 2 | Initialize mt19937 engine | Seed=123456789 |
| 3 | Set distribution range | 1 to 100 |
| 4 | Generate random number | 57 |
| 5 | Print output | Random number between 1 and 100: 57 |
Why This Works
Step 1: Seed the engine
The std::random_device provides a random seed to initialize the engine, ensuring different results each run.
Step 2: Use Mersenne Twister engine
The std::mt19937 engine generates high-quality pseudo-random numbers efficiently.
Step 3: Define distribution range
The std::uniform_int_distribution limits the random numbers to the desired range, here 1 to 100.
Step 4: Generate and print
Calling the distribution with the engine produces a random number, which is then printed.
Alternative Approaches
#include <iostream> #include <cstdlib> #include <ctime> int main() { std::srand(std::time(nullptr)); // Seed int random_number = std::rand() % 100 + 1; // 1 to 100 std::cout << "Random number between 1 and 100: " << random_number << std::endl; return 0; }
#include <iostream> #include <random> int main() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dist(0.0, 1.0); double random_number = dist(gen); std::cout << "Random real number between 0 and 1: " << random_number << std::endl; return 0; }
Complexity: O(1) time, O(1) space
Time Complexity
Generating a random number is a constant time operation because it involves a fixed number of steps without loops.
Space Complexity
Only a few variables for the engine and distribution are used, so space is constant.
Which Approach is Fastest?
Using rand() is slightly faster but less random; <random> provides better randomness with negligible performance cost.
| Approach | Time | Space | Best For |
|---|---|---|---|
| O(1) | O(1) | High-quality random numbers | |
| rand() with srand() | O(1) | O(1) | Simple, legacy code |
| uniform_real_distribution | O(1) | O(1) | Random floating-point numbers |
std::random_device to get different random numbers each run.