Random number generator (included): Difference between revisions

Added C++ section
(Added C++ section)
Line 130:
 
* <math>r_{n + 1} = 25214903917 \times r_n + 11 \pmod {2^{48}}</math>
 
=={{header|C++}}==
As part of the C++11 specification the language now includes various forms of random number generation.
 
While the default engine is implementation specific (ex, unspecified), the following Pseudo-random generators are available in the standard:
* Linear congruential (minstd_rand0, minstd_rand)
* Mersenne twister (mt19937, mt19937_64)
* Subtract with carry (ranlux24_base, ranlux48_base)
* Discard block (ranlux24, ranlux48)
* Shuffle order (knuth_b)
 
Additionally, the following distributions are supported:
* Uniform distributions: uniform_int_distribution, uniform_real_distribution
* Bernoulli distributions: bernoulli_distribution, geometric_distribution, binomial_distribution, negative_binomial_distribution
* Poisson distributions: poisson_distribution, gamma_distribution, exponential_distribution, weibull_distribution, extreme_value_distribution
* Normal distributions: normal_distribution, fisher_f_distribution, cauchy_distribution, lognormal_distribution, chi_squared_distribution, student_t_distribution
* Sampling distributions: discrete_distribution, piecewise_linear_distribution, piecewise_constant_distribution
 
Example of use:
{{works with|C++11}}
<lang cpp>#include <iostream>
#include <string>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::uniform_int_distribution<int> dist(1, 10);
std::mt19937 mt(rd());
std::cout << "Random Number (hardware): " << dist(rd) << std::endl;
std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
}</lang>
 
=={{header|C sharp}}==
Anonymous user