Search code examples
c++randomuniform-distribution

How to use std::uniform_int_distribution<T>::param()?


I would like to be able to reset the lower and upper bounds of a std::uniform_int_distribution<T> object after it has been created, and it seems the param function in the class allows this functionality. See https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution/param (2).

But it's not clear to me how to construct this param_type object that it takes in. Could someone provide an example?


Solution

  • It's not very clear from the documentation, but here it is:

    auto d = std::uniform_int_distribution<int>{1, 10};
    
    d.param(std::uniform_int_distribution<int>::param_type{11, 24});
    

    Here's how you can find this information in the docs:

    std::uniform_int_distribution

    Member types

    param_type: the type of the parameter set, see RandomNumberDistribution.

    RandomNumberDistribution

    P, the type named by D::param_type, which

    • [...]
    • has a constructor taking identical arguments as each of the constructors of D that take arguments corresponding to the distribution parameters.

    So basically std::uniform_int_distribution<int>::param_type has a constructor taking identical arguments as std::uniform_int_distribution<int>.