C++ | Compiler support | Freestanding and hosted | Language | Standard library | Standard library headers | Named requirements | Feature test macros | Language support library | Concepts library | Diagnostics library | Memory management library | Metaprogramming library | Containers library | Iterators library | Ranges library | Algorithms library | Strings library | Text processing library | Numerics library | Date and time library | Input/output library | Filesystem library | Concurrency support library | Execution control library | Technical specifications | Symbols index | [edit]
From cppreference.com
template<classIntType=int>classuniform_int_distribution;(since C++11)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) = 1b − a + 1.std::uniform_int_distribution satisfies all requirements of
.
Template parameters
IntType - The result type generated by the generator. The effect is undefined if this is not one of short, int, long, longlong, unsignedshort, unsignedint, unsignedlong, or unsignedlonglong. Member types
Member functions
constructs new distribution
(public member function)
resets the internal state of the distribution
(public member function)
Generation
generates the next random number in the distribution
(public member function)
Characteristics
returns the distribution parameters
(public member function)
gets or sets the distribution parameter object
(public member function)
returns the minimum potentially generated value
(public member function)
returns the maximum potentially generated value
(public member function)
Non-member functions
Example
This program simulates throwing 6-sided
.
Run this code
#include<iostream>#include<random>intmain(){std::random_devicerd;// a seed source for the random number enginestd::mt19937gen(rd());// mersenne_twister_engine seeded with rd()std::uniform_int_distribution<>distrib(1,6);// Use distrib to transform the random unsigned int// generated by gen into an int in [1, 6]for(intn=0;n!=10;++n)std::cout<<distrib(gen)<<' ';std::cout<<'\n';}Possible output:
1 1 6 5 2 2 5 5 6 2