Can I change a distribution parameters?

后端 未结 2 896
一整个雨季
一整个雨季 2021-01-04 11:30

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

<         


        
相关标签:
2条回答
  • 2021-01-04 11:36

    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

    0 讨论(0)
  • 2021-01-04 11:52

    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);
    
    0 讨论(0)
提交回复
热议问题