Use in a c++ class

前端 未结 2 1307
情歌与酒
情歌与酒 2021-01-14 06:35

I want to use the library in my program and I will have classes with different distributions and I want to generate a number at different times i

2条回答
  •  一整个雨季
    2021-01-14 07:19

    As drescherjm pointed out, dice is a local variable within he ctor. You need to make it accessible outside the scope of the ctor.I have tried to rework your program here. I think what you want is a random number generator that generates integer values from 0 to MR ? If that is the case, you can use the reworked program below:

     #include 
     #include 
     #include 
        class enemy {
        private:
            std::random_device rd;
            int max_roll;
            typedef std::mt19937 MyRng;
            MyRng rng;
           std::uniform_int_distribution dice;
        public:
           enemy(int MR) : max_roll(MR), rng(rd()), dice(std::uniform_int_distribution<>(1, MR)){
            rng.seed(::time(NULL));
            }
    
            int roll() {
                return dice(rng);
            }
        };
    
        int main()
        {
          enemy en(6);
          std::cout << "Roll dice produced : " << en.roll() << std::endl;
          return 0;
        }
    

    The program is self-explanatory. Please let me know if it is not and I can take you through it.

提交回复
热议问题