Use std::uniform_int_distribution and define its range later

c++, c++11, random

Solution

You can just do

group.dis = std::uniform_int_distribution<>(0,19);

or

group.dis.param(std::uniform_int_distribution<>::param_type(0,19));

Another way would be to add a method to your struct

struct group_s {
    int k;
    std::uniform_int_distribution<> dis;
    void set(int a, int b) { dis = std::uniform_int_distribution<>(a,b); }
} group;


group.set(0,19);

Problem

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?

Original source