C++ Random Number Generator
Learn how to generate random numbers in C++ with interactive code examples. Explore rand(), srand(), <random> library, std::uniform_int_distribution, and std::mt19937 with detailed explanations.
Need a Quick Random Number?
Try our online random number generator for instant results without writing any code.
C++ Code Editor
Learn how to generate random numbers in C++ with interactive code examples. Explore rand(), srand(), <random> library, std::uniform_int_distribution, and std::mt19937 with detailed explanations.
Output
Simulated Output (C++ runs on server)
Click "Run Code" to see example output
Classic C++ with rand()
Use the traditional rand() function from <cstdlib> to generate random integers. Simple and widely supported, though not suitable for cryptographic applications due to poor randomness quality.
Understanding C++ Random Number Generation
C++ provides multiple ways to generate random numbers, evolving from the simple rand() function in C to the modern <random> library in C++11. The classic rand() uses a Linear Congruential Generator (LCG) and is seeded with srand(), while the modern approach uses the Mersenne Twister algorithm and provides various distribution classes for different use cases.
rand() Function
The classic rand() function from <cstdlib> provides simple pseudo-random number generation. Seeded with srand(), it uses a Linear Congruential Generator (LCG) algorithm. Suitable for basic applications but not for cryptographic purposes.
<random> Library
The <random> library (C++11+) provides modern random number generation with std::mt19937 (Mersenne Twister), various distributions like uniform_int_distribution, and better randomness quality than rand().
uniform_int_distribution
std::uniform_int_distribution provides precise control over integer ranges with uniform distribution. Perfect for statistical applications, simulations, and when exact range control is needed.
std::mt19937
std::mt19937 implements the Mersenne Twister algorithm, providing excellent statistical properties and a very long period (2^19937-1). It is the default engine for most <random> distributions.
Common Use Cases
Gaming & Simulations
Generate random dice rolls, card draws, procedural generation, and game events using rand() or modern <random> library in game loops and simulations.
Security & Cryptography
For security applications, consider using specialized cryptographic libraries. The standard rand() is not cryptographically secure. Use platform-specific secure random number generators for passwords and tokens.
High-Performance Computing
The modern <random> library with std::mt19937 provides excellent performance for generating large quantities of random numbers in HPC and scientific computing applications.
Software Testing
Create randomized test data, fuzz testing inputs, and edge cases to ensure robust code coverage and bug detection in C++ applications.
How C++ Random Number Generation Works
C++'s classic rand() function (from next = (a * current + c) % m. The sequence is deterministic and must be seeded with srand().
The modern library (C++11+) uses the Mersenne Twister algorithm (std::mt19937), which provides much better randomness quality and statistical properties. It also offers various distribution classes like uniform_int_distribution, normal_distribution, and bernoulli_distribution for different use cases.
std::random_device provides a platform-specific source of entropy for seeding the random number engines, ensuring different sequences each time the program runs.
Expert Tips for C++ Random Generation
Avoid rand() for New Code
For new C++ code (C++11 and later), prefer the
Always Seed srand()
If using rand(), always call srand(std::time(0)) at program start to seed the generator. Otherwise, you'll get the same sequence every time. With
Use Appropriate Distributions
For uniform integer ranges, use std::uniform_int_distribution. For floating-point numbers, use std::uniform_real_distribution. This ensures proper statistical properties.
Technical Specifications
Algorithms
- Linear Congruential Generator (rand function)
- Mersenne Twister (std::mt19937)
- Platform-specific entropy source (std::random_device)
Performance Characteristics
- rand(): Fast but poor randomness quality
- <random>: Fast with excellent quality
- std::mt19937: Very fast for large quantities