SysTutorials

Linux Manual Pages › Section 3

stduniform_int_distribution(3)

std::uniform_int_distribution - std::uniform_int_distribution

Synopsis

Defined in header<random>
    template <class IntType = int>
    (since C++ 11)
        class uniform_int_distribution;

Produces random integer values i, uniformly distributed on the closed interval [a, b], that is, distributed according to the discrete probability function       P(i|a,b) =       1       b − a + 1       . std::uniform_int_distribution satisfies all requirements of RandomNumberDistribution

Template parameters

IntType - The result type generated by the generator. The effect is undefined if this is not one of short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long.

Member types

Member type Definition result_type IntType param_type the type of the parameter set, see RandomNumberDistribution.

Member functions

              constructs new distribution constructor (public member function)               resets the internal state of the distribution reset (public member function)

Generation

              generates the next random number in the distribution operator() (public member function)

Characteristics

              returns the distribution parameters a (public member function) b               gets or sets the distribution parameter object param (public member function)               returns the minimum potentially generated value min (public member function)               returns the maximum potentially generated value max (public member function)

Non-member functions

           compares two distribution objects operator== (function) operator!=            performs stream input and output on pseudo-random number distribution operator<< (function template) operator>>

Example

This program simulates throwing 6-sided dice. // Run this code

#include <random>
#include <iostream>
int main()
{
std::random_device rd;  // Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); // Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> dis(1, 6);
for (int n = 0; n < 10; ++n)
// Use dis to transform the random unsigned int generated by gen into an int in [1, 6]
std::cout << dis(gen) << ’ ‘;
std::cout << ‘\n’;
}

Possible output:

  1 1 6 5 2 2 5 5 6 2