A random number supports a guessing game by producing a value within a chosen range. C++ sets this up with a seed, an engine, and a distribution. section 3.0 section 3.2.2
A single generated value follows this pattern:
- Seed an engine with std::random_device. section 3.2.2
- Create an std::mt19937 engine and a uniform_int_distribution<int> for the desired range. section 3.2.2
- Call dist(engine); repeat the call without repeating setup when more values are needed. section 3.2.2
Random prime generation
To generate a prime, draw values from 1 to 99,999 and keep trying until is_prime accepts one. This filtering technique is rejection sampling. section 3.3.3
Listing 3.9 Generating a single random number
int some_random_number()
{
std::random_device rd; #1
std::mt19937 engine(rd()); #2
std::uniform_int_distribution<int> dist(1, 100); #3
return dist(engine); #4
}
View the complete C++ function for generating one uniformly distributed integer from 1 to 100. section 3.2.2



