The C++ standard library
The C++ standard library's The header provides a set of tools for generating random numbers, ranging from simple uniform distributions to complex discrete distributions, offering a wide range of choices for applications that need random numbers. These tools are very useful in fields such as simulation, game development, and cryptographic algorithms.
It not only supports generating pseudo-random numbers, but also supports seed control, various probability distributions, etc., allowing developers to flexibly generate random number sequences that meet specific needs.
The library consists of the following three main components:
- Random number engine: The core for generating pseudo-random numbers, used to control the repeatability and randomness of the generation process.
- Random number distribution: Controls the type of probability distribution that the generated values follow.
- Random number adapter: Allows adjusting the engine behavior, such as
discard_blockand other adapters.
In C++, random number generators (RNG) can be divided into two major categories:
- Pseudo-random number generator: They use deterministic algorithms to generate seemingly random sequences. These sequences are theoretically predictable, but are usually random enough for most applications.
- True random number generator: They generate random numbers based on physical processes (such as thermal noise, radioactive decay, etc.), but the C++ standard library does not directly provide such generators.
Commonly used random number engines
| Engine | Description |
|---|---|
std::default_random_engine |
Default random number engine; the implementation depends on the specific compiler. |
std::minstd_rand |
Linear congruential engine, producing a uniform pseudo-random number sequence. |
std::mt19937 |
Mersenne Twister algorithm, suitable for general-purpose random number generation. |
std::mt19937_64 |
64-bit Mersenne Twister algorithm. |
std::ranlux24_base |
Simplified subtract-with-carry engine, used for high-quality generation. |
std::knuth_b |
Knuth shuffle random number generator. |
Commonly used engines such as std::mt19937 because they generate quickly and with high quality, are the generally recommended random number generation engines. The generator can also use the seed() method to specify a seed, making it easy to generate reproducible pseudo-random sequences.
Random number distribution types
1. Uniform distribution
| distribution | Description |
|---|---|
std::uniform_int_distribution |
generates a uniform distribution over a certain integer range. |
std::uniform_real_distribution |
generates a uniform distribution over a certain floating-point range. |
std::mt19937 gen(seed);
std::uniform_int_distribution dist(1, 100); // 生成 1 到 100 间的整数
int random_int = dist(gen);
2. Normal distribution
| distribution | Description |
|---|---|
std::normal_distribution |
Standard normal distribution, symmetric around the center, commonly used to simulate natural phenomena. |
std::lognormal_distribution |
Lognormal distribution. |
std::mt19937 gen(seed);
std::normal_distribution<> dist(0, 1); // 平均值为0,标准差为1
double random_normal = dist(gen);
3. Discrete distribution
| distribution | Description |
|---|---|
std::discrete_distribution |
generates random numbers that are integers with a set of specific probabilities. |
std::bernoulli_distribution |
Bernoulli distribution, only generates true or false. |
std::binomial_distribution |
binomial distribution. |
std::mt19937 gen(seed);
std::discrete_distribution dist({40, 10, 50}); // 概率为40%、10%、50%
int random_discrete = dist(gen);
Syntax
Use The basic steps of the library are as follows:
- Including the header
. - Create a random number generator object.
- Use a distribution class to generate random numbers.
Example
Generate basic random numbers
Example
#include
#include
int
main
(
)
{
// Use a random device to create a random seed
std
::
random_device
rd
;
// Initialize the Mersenne Twister random number generator with the random seed
std
::
mt19937
generator
(
rd
(
)
)
;
// Generate a random number
std
::
cout
<<
"Random number: "
<<
generator
(
)
<<
std
::
endl
;
return
0
;
}
Output result:
Random number: 3499211612
**Note:** Each time the program runs, the generated random numbers may differ.
Using a uniform distribution
Example
#include
#include
int
main
(
)
{
// Create a random number generator
std
::
mt19937
generator
;
// Create a uniform distribution random number generator, ranging from 1 to 10
std
::
uniform_int_distribution
<
int
distribution
(
1
,
10
);
// Generate and print 5 random numbers
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
std
::
cout
<<
"Random number: "
<<
distribution
(
generator
)
<<
std
::
endl
;
}
return
0
;
}
Output result:
Random number: 7
Random number: 10
Random number: 6
Random number: 2
Random number: 4
Each time the program runs, the output random number sequence may differ.
Using a normal distribution
Example
#include
#include
#include
int
main
(
)
{
// Create a random number generator
std
::
mt19937
generator
;
// Create a normal-distribution random number generator with mean 0 and standard deviation 1
std
::
normal_distribution
<
double
distribution
(
0.0
,
1.0
);
// Set the output format to keep two decimal places
std
::
cout
<<
std
::
fixed
<<
std
::
setprecision
(
2
)
;
// Generate and print 5 random numbers
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
std
::
cout
<<
"Random number: "
<<
distribution
(
generator
)
<<
std
::
endl
;
}
return
0
;
}
Random number: -0.15
Random number: 0.13
Random number: -1.87
Random number: 0.46
Random number: -0.21
Using the mt19937 engine to generate random numbers of different distributions:
Example
#include
#include
int
main
(
)
{
// 1. Set the seed and random number engine
std
::
random_device
rd
;
// A random device generates the seed
std
::
mt19937
gen
(
rd
(
)
)
;
// Mersenne Twister engine
// 2. Uniform distribution of integers
std
::
uniform_int_distribution
<>
dist_int
(
1
,
100
)
;
std
::
cout
<<
"Uniform integer: "
<<
dist_int
(
gen
)
<<
std
::
endl
;
// 3. Uniform distribution of floating-point numbers
std
::
uniform_real_distribution
<>
dist_real
(
0.0
,
1.0
)
;
std
::
cout
<<
"Uniform real: "
<<
dist_real
(
gen
)
<<
std
::
endl
;
// 4. Normal distribution
std
::
normal_distribution
<>
dist_normal
(
0
,
1
)
;
std
::
cout
<<
"Normal: "
<<
dist_normal
(
gen
)
<<
std
::
endl
;
// 5. Discrete distribution
std
::
discrete_distribution
<>
dist_discrete
(
{
10
,
20
,
70
}
)
;
std
::
cout
<<
"Discrete: "
<<
dist_discrete
(
gen
)
<<
std
::
endl
;
return
0
;
}
Random number adapter
Random number adapters allow adjusting the generation behavior; there are mainly two types of adapters:
std::discard_block_engine: Discards a certain number of generated values, keeping only values at specified intervals, used to reduce randomness.std::independent_bits_engine: Generates random numbers with a specified number of bits, convenient for directly generating binary data.
Example
#include
#include
int
main
(
)
{
std
::
random_device
rd
;
std
::
mt19937
gen
(
rd
(
)
)
;
std
::
discard_block_engine
<
std
::
mt19937
,
3
,
2
discard_engine
(
gen
)
;
std
::
cout
<<
"Discard block random number: "
<<
discard_engine
(
)
<<
std
::
endl
;
return
0
;
}
Common usage
- Use a seed to generate a reproducible random sequence: Specifying the same seed makes each run generate the same sequence.
- Generate random numbers of different distributions: Choosing a distribution function that fits the application scenario can better simulate real problems.
- Adjust generator performance: Use different random engines to balance generation performance and precision.