GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM
  • C library:
  • <cassert> (assert.h)
  • <cctype> (ctype.h)
  • <cerrno> (errno.h)
  • <cfenv> (fenv.h)
  • <cfloat> (float.h)
  • <cinttypes> (inttypes.h)
  • <ciso646> (iso646.h)
  • <climits> (limits.h)
  • <clocale> (locale.h)

This view

  • Original page
  • New address

GeezerVison is showing cplusplus.com — <random>

  • TUTORIALS
  • REFERENCE
  • ARTICLES
  • FORUM

C++

  • Tutorials
  • Reference
  • Articles
  • Forum

Reference

  • C library:

    • <cassert> (assert.h)
    • <cctype> (ctype.h)
    • <cerrno> (errno.h)
    • C++11
      <cfenv> (fenv.h)
    • <cfloat> (float.h)
    • C++11
      <cinttypes> (inttypes.h)
    • <ciso646> (iso646.h)
    • <climits> (limits.h)
    • <clocale> (locale.h)
    • <cmath> (math.h)
    • <csetjmp> (setjmp.h)
    • <csignal> (signal.h)
    • <cstdarg> (stdarg.h)
    • C++11
      <cstdbool> (stdbool.h)
    • <cstddef> (stddef.h)
    • C++11
      <cstdint> (stdint.h)
    • <cstdio> (stdio.h)
    • <cstdlib> (stdlib.h)
    • <cstring> (string.h)
    • C++11
      <ctgmath> (tgmath.h)
    • <ctime> (time.h)
    • C++11
      <cuchar> (uchar.h)
    • <cwchar> (wchar.h)
    • <cwctype> (wctype.h)
  • Containers:

    • C++11
      <array>
    • <deque>
    • C++11
      <forward_list>
    • <list>
    • <map>
    • <queue>
    • <set>
    • <stack>
    • C++11
      <unordered_map>
    • C++11
      <unordered_set>
    • <vector>
  • Input/Output:

    • <fstream>
    • <iomanip>
    • <ios>
    • <iosfwd>
    • <iostream>
    • <istream>
    • <ostream>
    • <sstream>
    • <streambuf>
  • Multi-threading:

    • C++11
      <atomic>
    • C++11
      <condition_variable>
    • C++11
      <future>
    • C++11
      <mutex>
    • C++11
      <thread>
  • Other:

    • <algorithm>
    • <bitset>
    • C++11
      <chrono>
    • C++11
      <codecvt>
    • <complex>
    • <exception>
    • <functional>
    • C++11
      <initializer_list>
    • <iterator>
    • <limits>
    • <locale>
    • <memory>
    • <new>
    • <numeric>
    • C++11
      <random>
    • C++11
      <ratio>
    • C++11
      <regex>
    • <stdexcept>
    • <string>
    • C++11
      <system_error>
    • C++11
      <tuple>
    • C++11
      <type_traits>
    • C++11
      <typeindex>
    • <typeinfo>
    • <utility>
    • <valarray>

<random>

  • distributions

    • C++11
      bernoulli_distribution
    • C++11
      binomial_distribution
    • C++11
      cauchy_distribution
    • C++11
      chi_squared_distribution
    • C++11
      discrete_distribution
    • C++11
      exponential_distribution
    • C++11
      extreme_value_distribution
    • C++11
      fisher_f_distribution
    • C++11
      gamma_distribution
    • C++11
      geometric_distribution
    • C++11
      lognormal_distribution
    • C++11
      negative_binomial_distribution
    • C++11
      normal_distribution
    • C++11
      piecewise_constant_distribution
    • C++11
      piecewise_linear_distribution
    • C++11
      poisson_distribution
    • C++11
      student_t_distribution
    • C++11
      uniform_int_distribution
    • C++11
      uniform_real_distribution
    • C++11
      weibull_distribution
  • generators

    • C++11
      default_random_engine
    • C++11
      discard_block_engine
    • C++11
      independent_bits_engine
    • C++11
      knuth_b
    • C++11
      linear_congruential_engine
    • C++11
      mersenne_twister_engine
    • C++11
      minstd_rand
    • C++11
      minstd_rand0
    • C++11
      mt19937
    • C++11
      mt19937_64
    • C++11
      random_device
    • C++11
      ranlux24
    • C++11
      ranlux24_base
    • C++11
      ranlux48
    • C++11
      ranlux48_base
    • C++11
      shuffle_order_engine
    • C++11
      subtract_with_carry_engine
  • other

    • C++11
      generate_canonical
    • C++11
      seed_seq
  • Reference
  • <random>
header

<random>

Random
This header introduces random number generation facilities.

This library allows to produce random numbers using combinations of generators and distributions:
  • Generators: Objects that generate uniformly distributed numbers.
  • Distributions: Objects that transform sequences of numbers generated by a generator into sequences of numbers that follow a specific random variable distribution, such as uniform, Normal or Binomial.

Distribution objects generate random numbers by means of their operator() member, which takes a generator object as argument:
1
2
3
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(1,6);
int dice_roll = distribution(generator);  // generates number in the range 1..6 

For repeated uses, both can be bound together:
1
2
auto dice = std::bind ( distribution, generator );
int wisdom = dice()+dice()+dice();

Except for random_device, all standard generators defined in the library are random number engines, which are a kind of generators that use a particular algorithm to generate series of pseudo-random numbers. These algorithms need a seed as a source of randomness, and this seed can either be a single value or an object with a very specific generate() member function (see seed_seq for more info). A typical source of randomness for trivial tasks is time, such as the information provided by time or system_clock::now (for a typical example, see uniform_int_distribution::operator()).

As an alternative, trivial random numbers can also be generated using cstdlib's functions rand and srand.

Generators

Pseudo-random number engines (templates)

Generators that use an algorithm to generate pseudo-random numbers based on an initial seed:
linear_congruential_engine
Linear congruential random number engine (class template)
mersenne_twister_engine
Mersenne twister random number engine (class template)
subtract_with_carry_engine
Subtract-with-carry random number engine (class template)

Engine adaptors

They adapt an engine, modifying the way numbers are generated with it:
discard_block_engine
Discard-block random number engine adaptor (class template)
independent_bits_engine
Independent-bits random number engine adaptor (class template)
shuffle_order_engine
Shuffle-order random number engine adaptor (class template)

Pseudo-random number engines (instantiations)

Particular instantiations of generator engines and adaptors:
default_random_engine
Default random engine (class)
minstd_rand
Minimal Standard minstd_rand generator (class)
minstd_rand0
Minimal Standard minstd_rand0 generator (class)
mt19937
Mersenne Twister 19937 generator (class)
mt19937_64
Mersenne Twister 19937 generator (64 bit) (class)
ranlux24_base
Ranlux 24 base generator (class)
ranlux48_base
Ranlux 48 base generator (class)
ranlux24
Ranlux 24 generator (class)
ranlux48
Ranlux 48 generator (class)
knuth_b
Knuth-B generator (class)

Random number generators

Non-deterministic random number generator:
random_device
True random number generator (class)

Distributions

Uniform:

uniform_int_distribution
Uniform discrete distribution (class template)
uniform_real_distribution
Uniform real distribution (class template)

Related to Bernoulli (yes/no) trials:

bernoulli_distribution
Bernoulli distribution (class)
binomial_distribution
Binomial distribution (class template)
geometric_distribution
Geometric distribution (class template)
negative_binomial_distribution
Negative binomial distribution (class template)

Rate-based distributions:

poisson_distribution
Poisson distribution (class template)
exponential_distribution
Exponential distribution (class template)
gamma_distribution
Gamma distribution (class template)
weibull_distribution
Weibull distribution (class template)
extreme_value_distribution
Extreme Value distribution (class template)

Related to Normal distribution:

normal_distribution
Normal distribution (class template)
lognormal_distribution
Lognormal distribution (class template)
chi_squared_distribution
Chi-squared distribution (class template)
cauchy_distribution
Cauchy distribution (class template)
fisher_f_distribution
Fisher F-distribution (class template)
student_t_distribution
Student T-Distribution (class template)

Piecewise distributions:

discrete_distribution
Discrete distribution (class template)
piecewise_constant_distribution
Piecewise constant distribution (class template)
piecewise_linear_distribution
Piecewise linear distribution (class template)

Other

seed_seq
Seed sequence (class)
generate_canonical
Generate canonical numbers (function template)
Home page | Privacy policy
© cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s
Spotted an error? contact us
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.