std::uniform_int_distribution (3) Linux Manual Page
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
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
constructor (public member function)
reset (public member function)
Generation
operator() (public member function)
Characteristics
a (public member function)
b
param (public member function)
min (public member function)
max (public member function)
Non-member functions
operator== (function)
operator!=
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:
