Export (0) Print
Expand All

uniform_int::operator()

Returns a random value.

template<class Engine>
    result_type operator()(Engine& eng);
template<class Engine>
    result_type operator()(Engine& eng, result_type n);

Engine

The type of the random engine.

eng

The random engine.

n

The upper bound for random values.

The first member function uses the engine eng as a source of uniformly distributed integral values and returns integral values with each value i in the closed range [min(), max()] occurring with equal probability and values outside that range occuring with probability 0.

The second member function uses the engine eng as a source of uniformly distributed integral values and returns integral values with each value i in the half-open range [0, n) occurring with equal probability and values outside that range occuring with probability 0.

 

// std_tr1__random__uniform_int_operator_fn.cpp 
// compile with: /EHsc 
#include <random> 
#include <iostream> 
 
typedef std::mt19937 Myeng; 
typedef std::uniform_int<unsigned int> Myceng; 
int main() 
    { 
    Myeng eng; 
    Myceng ceng(1, 1 << 15); 
    Myceng::input_type engval = eng(); 
    Myceng::result_type compval = ceng(eng); 
 
    compval = compval;  // to quiet "unused" warnings 
    engval = engval; 
 
    std::cout << "min == " << ceng.min() << std::endl; 
    std::cout << "max == " << ceng.max() << std::endl; 
 
    ceng.reset(); // discard any cached values 
    std::cout << "a random value == " << ceng(eng) << std::endl; 
    std::cout << "a random value == " << ceng(eng) << std::endl; 
    std::cout << "a random value == " << ceng(eng) << std::endl; 
 
    return (0); 
    } 
 
min == 1
max == 32768
a random value == 29681
a random value == 27362
a random value == 4162

Header: <random>

Namespace: std

Community Additions

ADD
Show:
© 2014 Microsoft