A.3.1. Random Number Distributions

With the exception of the bernouilli_distribution, which always generates type bool, the distribution types are templates. Each of these templates takes a single type parameter that names the result type that the distribution will generate.

The distribution classes differ from other class templates we’ve used in that the distribution types place restrictions on the types we can specify for the template type. Some distribution templates can be used to generate only floating-point numbers; others can be used to generate only integers.

In the following descriptions, we indicate whether a distribution generates floating-point numbers by specifying the type as template_name <RealT>. For these templates, we can use float, double, or long double in place of RealT. Similarly, IntT requires one of the built-in integral types, not including bool or any of the char types. The types that can be used in place of IntT are short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.

The distribution templates define a default template type parameter (§ 17.4.2, p. 750). The default for the integral distributions is int; the default for the classes that generate floating-point numbers is double.

The constructors for each distribution has parameters that are specific to the kind of distribution. Some of these parameters specify the range of the distribution. These ranges are always inclusive, unlike iterator ranges.

Uniform Distributions

uniform_int_distribution<IntT> u(m, n);
uniform_real_distribution<RealT> u(x, y);

Generates values of the specified type in the given inclusive range. m (or x) is the smallest number that can be returned; n (or y) is the largest. m defaults to 0; n defaults to the maximum value that can be represented in an object of type IntT. x defaults to 0.0 and y defaults to 1.0.

Bernoulli Distributions

bernoulli_distribution b(p);

Yields true with given probability p; p defaults to 0.5.

binomial_distribution<IntT> b(t, p);

Distribution computed for a sample size that is the integral value t, with probability p; t defaults to 1 and p defaults to 0.5.

geometric_distribution<IntT> g(p);

Per-trial probability of success p; p defaults to 0.5.

negative_binomial_distribution<IntT> nb(k, p);

Integral value k trials with probability of success p; k defaults to 1 and p to 0.5.

Poisson Distributions

poisson_distribution<IntT> p(x);

Distribution around double mean x.

exponential_distribution<RealT> e(lam);

Floating-point valued lambda lam; lam defaults to 1.0.

gamma_distribution<RealT> g(a, b);

With alpha (shape) a and beta (scale) b; both default to 1.0.

weibull_distribution<RealT> w(a, b);

With shape a and scale b; both default to 1.0.

extreme_value_distribution<RealT> e(a, b);

a defaults to 0.0 and b defaults to 1.0.

Normal Distributions

normal_distribution<RealT> n(m, s);

Mean m and standard deviation s; m defaults to 0.0, s to 1.0.

lognormal_distribution<RealT> ln(m, s);

Mean m and standard deviation s; m defaults to 0.0, s to 1.0.

chi_squared_distribution<RealT> c(x);

x degrees of freedom; defaults to 1.0.

cauchy_distribution<RealT> c(a, b);

Location a and scale b default to 0.0 and 1.0, respectively.

fisher_f_distribution<RealT> f(m, n);

m and n degrees of freedom; both default to 1.

student_t_distribution<RealT> s(n);

n degrees of freedom; n defaults to 1.

Sampling Distributions

discrete_distribution<IntT> d(i, j);
discrete_distribution<IntT> d{il};

i and j are input iterators to a sequence of weights; il is a braced list of weights. The weights must be convertible to double.

piecewise_constant_distribution<RealT> pc(b, e, w);

b, e, and w are input iterators.

piecewise_linear_distribution<RealT> pl(b, e, w);

b, e, and w are input iterators.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.35.193