Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
|
|
The random number library provides classes that generate random and pseudo-random numbers. These classes include:
URBGs and distributions are designed to be used together to produce random values. All of the random number engines may be specifically seeded, serialized, and de-serialized for use with repeatable simulators.
Contents |
Auniform random bit generator is a function object returning unsigned integer values such that each value in the range of possible results has (ideally) equal probability of being returned.
All uniform random bit generators meet theUniformRandomBitGenerator requirements.C++20 also defines auniform_random_bit_generator
concept.
Defined in header <random> | |
(C++20) | specifies that a type qualifies as a uniform random bit generator (concept)[edit] |
Arandom number engine (commonly shortened toengine ) is a uniform random bit generator which generates pseudo-random numbers using seed data as entropy source.
At any given time, an enginee of typeE
has a stateei
for some non-negative integeri. Upon construction,e has an initial statee0
, which is determined by engine parameters and an initial seed (or seed sequence).
The following properties are always defined for any engine typeE
:
E
’s state in multiples of the size ofE::result_type
(i.e.(sizeof ei
)/ sizeof(E::result_type)).i
is advanced to its successor stateei+1
(i.e.TA(ei
)== ei+1
).E::result_type
, the result is a pseudo-random number.A pseudo-random number sequence can be generated by callingTA andGA alternatively.
The standard library provides the implementations of three different classes of pseudo-random number generation algorithms as class templates, so that the algorithms can be customized. The choice of which engine to use involves a number of trade-offs:
| (since C++26) |
None of these random number engines arecryptographically secure. As with any secure operation, a crypto library should be used for the purpose (e.g.OpenSSLRAND_bytes
).
All types instantiated from these templates meet theRandomNumberEngine requirements.
Defined in header <random> | |
(C++11) | implementslinear congruential algorithm (class template)[edit] |
(C++11) | implementsMersenne twister algorithm (class template)[edit] |
(C++11) | implements a subtract-with-carry (lagged Fibonacci) algorithm (class template)[edit] |
(C++26) | a counter-based parallelizable generator (class template)[edit] |
Random number engine adaptors generate pseudo-random numbers using another random number engine as entropy source. They are generally used to alter the spectral characteristics of the underlying engine.
Defined in header <random> | |
(C++11) | discards some output of a random number engine (class template)[edit] |
(C++11) | packs the output of a random number engine into blocks of a specified number of bits (class template)[edit] |
(C++11) | delivers the output of a random number engine in a different order (class template)[edit] |
Several specific popular algorithms are predefined.
Defined in header <random> | |
Type | Definition |
minstd_rand0 (C++11) | std::linear_congruential_engine<std::uint_fast32_t, 16807,0,2147483647>Discovered in 1969 by Lewis, Goodman and Miller, adopted as "Minimal standard" in 1988 by Park and Miller[edit] |
minstd_rand (C++11) | std::linear_congruential_engine<std::uint_fast32_t, |
mt19937 (C++11) | std::mersenne_twister_engine<std::uint_fast32_t, |
mt19937_64 (C++11) | std::mersenne_twister_engine<std::uint_fast64_t, |
ranlux24_base (C++11) | std::subtract_with_carry_engine<std::uint_fast32_t,24,10,24>[edit] |
ranlux48_base (C++11) | std::subtract_with_carry_engine<std::uint_fast64_t,48,5,12>[edit] |
ranlux24 (C++11) | std::discard_block_engine<std::ranlux24_base,223,23> 24-bit RANLUX generator by Martin Lüscher and Fred James, 1994[edit] |
ranlux48 (C++11) | std::discard_block_engine<std::ranlux48_base,389,11> 48-bit RANLUX generator by Martin Lüscher and Fred James, 1994[edit] |
knuth_b (C++11) | std::shuffle_order_engine<std::minstd_rand0,256>[edit] |
philox4x32 (C++26) | std::philox_engine<std::uint_fast32_t,32,4,10, 0xCD9E8D57,0x9E3779B9, 0xD2511F53,0xBB67AE85>[edit] |
philox4x64 (C++26) | std::philox_engine<std::uint_fast64_t,64,4,10, 0xCA5A826395121157,0x9E3779B97F4A7C15, 0xD2E7470EE14C6C93,0xBB67AE8584CAA73B>[edit] |
default_random_engine (C++11) | an implementation-definedRandomNumberEngine type |
std::random_device is a non-deterministic uniform random bit generator, although implementations are allowed to implementstd::random_device using a pseudo-random number engine if there is no support for non-deterministic random number generation.
(C++11) | non-deterministic random number generator using hardware entropy source (class)[edit] |
A random number distribution post-processes the output of a URBG in such a way that resulting output is distributed according to a defined statistical probability density function.
Random number distributions satisfyRandomNumberDistribution.
Defined in header <random> | |
Uniform distributions | |
(C++11) | produces integer values evenly distributed across a range (class template)[edit] |
(C++11) | produces real values evenly distributed across a range (class template)[edit] |
Bernoulli distributions | |
(C++11) | producesbool values on aBernoulli distribution (class)[edit] |
(C++11) | produces integer values on abinomial distribution (class template)[edit] |
produces integer values on anegative binomial distribution (class template)[edit] | |
(C++11) | produces integer values on ageometric distribution (class template)[edit] |
Poisson distributions | |
(C++11) | produces integer values on aPoisson distribution (class template)[edit] |
(C++11) | produces real values on anexponential distribution (class template)[edit] |
(C++11) | produces real values on agamma distribution (class template)[edit] |
(C++11) | produces real values on aWeibull distribution (class template)[edit] |
(C++11) | produces real values on anextreme value distribution (class template)[edit] |
Normal distributions | |
(C++11) | produces real values on astandard normal (Gaussian) distribution (class template)[edit] |
(C++11) | produces real values on alognormal distribution (class template)[edit] |
(C++11) | produces real values on achi-squared distribution (class template)[edit] |
(C++11) | produces real values on aCauchy distribution (class template)[edit] |
(C++11) | produces real values on aFisher's F-distribution (class template)[edit] |
(C++11) | produces real values on aStudent's t-distribution (class template)[edit] |
Sampling distributions | |
(C++11) | produces integer values on a discrete distribution (class template)[edit] |
produces real values distributed on constant subintervals (class template)[edit] | |
produces real values distributed on defined subintervals (class template)[edit] |
Defined in header <random> | |
(C++11) | evenly distributes real values of given precision across[ 0, 1) (function template)[edit] |
(C++11) | general-purpose bias-eliminating scrambled seed sequence generator (class)[edit] |
Defined in header <random> | |
(C++26) | fills a range with random numbers from a uniform random bit generator (algorithm function object)[edit] |
In addition to the engines and distributions described above, the functions and constants from the C random library are also available though not recommended:
Defined in header <cstdlib> | |
generates a pseudo-random number (function)[edit] | |
seeds pseudo-random number generator (function)[edit] | |
maximum possible value generated bystd::rand (macro constant)[edit] |
#include <cmath>#include <iomanip>#include <iostream>#include <map>#include <random>#include <string> int main(){// Seed with a real random value, if availablestd::random_device r; // Choose a random mean between 1 and 6 std::default_random_engine e1(r());std::uniform_int_distribution<int> uniform_dist(1,6);int mean= uniform_dist(e1);std::cout<<"Randomly-chosen mean: "<< mean<<'\n'; // Generate a normal distribution around that meanstd::seed_seq seed2{r(), r(), r(), r(), r(), r(), r(), r()};std::mt19937 e2(seed2);std::normal_distribution<> normal_dist(mean,2); std::map<int,int> hist;for(int n=0; n!=10000;++n)++hist[std::round(normal_dist(e2))]; std::cout<<"Normal distribution around "<< mean<<":\n"<<std::fixed<<std::setprecision(1);for(auto[x, y]: hist)std::cout<<std::setw(2)<< x<<' '<<std::string(y/200,'*')<<'\n';}
Possible output:
Randomly-chosen mean: 4Normal distribution around 4:-4-3-2-1 0 * 1 *** 2 ****** 3 ******** 4 ********* 5 ******** 6 ****** 7 *** 8 * 9101112
C documentation forPseudo-random number generation |