vlad4378
vlad4378

Reputation: 893

Can I change a distribution parameters?

There is uniform_int_distribution in < random > When I creating that I define an interval. Can I change this interval after the creation?

for example

std::uniform_int_distribution distr(0, 10);
// can I change an interval here ?    

Upvotes: 18

Views: 6167

Answers (2)

dreamzor
dreamzor

Reputation: 5925

Just assign a new distribution to the variable:

std::uniform_int_distribution<int> distr(0, 10);

distr = std::uniform_int_distribution<int>(5, 13);

Or, create a parameter for that (@awesomeyi answer required distribution object creation, this still requires param_type object creation)

std::uniform_int_distribution<int> distr(0, 10); 

distr.param(std::uniform_int_distribution<int>::param_type(5, 13));

Proof that param_type will work (for @stefan):

P is the associated param_type. It shall satisfy the CopyConstructible, CopyAssignable, and EqualityComparable requirements. It also has constructors that take the same arguments of the same types as the constructors of D and has member functions identical to the parameter-returning getters of D

http://en.cppreference.com/w/cpp/concept/RandomNumberDistribution

Upvotes: 19

yizzlez
yizzlez

Reputation: 8805

You can through the param() function.

std::uniform_int_distribution<int> distr(0, 10);
std::uniform_int_distribution<int>::param_type d2(2, 10);
distr.param(d2);

Upvotes: 3

Related Questions