Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

c++ - Use std::uniform_int_distribution and define its range later

I have problem where I want to create a std::uniform_int_distribution in a struct and then give its range later. Below is what I want.

#include <random>
#include <iostream>

std::random_device rd;
std::mt19937 gen(rd());

struct group_s {
   int k;
   std::uniform_int_distribution<> dis;
} group;


int main()
{  
    group.dis(0,19);
    std::cout << group.dis(gen) << ' ';
}

I am getting the following error:

no match for call to '(std::uniform_int_distribution<>) (int, int)'
  cpu_group.dis(0,19);

How do I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use param():

using param_t = std::uniform_int_distribution<>::param_type;

group.dis.param(param_t(0, 19));

If the parameters change every time you use the distribution, then you can also consider using the two-parameter overload of operator() instead:

std::cout << group.dis(gen, param_t(0, 19)) << ' ';

As distribution objects are allowed to store extra bits of entropy obtained during a previous operator() call, this approach can be more efficient than constructing a new distribution object and assigning it.

Note that the cppreference page is incomplete and doesn't document the requirements the standard imposes on param_type. Given a distribution type D and its associated param_type P,

For each of the constructors of D taking arguments corresponding to parameters of the distribution, P shall have a corresponding constructor subject to the same requirements and taking arguments identical in number, type, and default values. Moreover, for each of the member functions of D that return values corresponding to parameters of the distribution, P shall have a corresponding member function with the identical name, type, and semantics.

(§26.5.1.6 [rand.req.dist]/p9)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...