Can I change the distribution parameters?

There is a uniform distribution in <random> When I create, I define an interval. Can I change this interval after creation?

eg

std::uniform_int_distribution distr(0, 10); // can I change an interval here ? 
+8
c ++ random
source share
2 answers

Just assign the new distribution to a variable:

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

Or create a parameter for this (@awesomeyi answer requires creating a distribution object, it still requires creating a param_type object)

 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 param_type associated parameter. It must satisfy the CopyConstructible, CopyAssignable and EqualityComparable requirements. It also has constructors that accept the same arguments of the same types as the D constructors, and have member functions identical to the returning parameters of the D getters

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

+10
source share

You can execute the param() function.

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

All Articles