Use in a c++ class

前端 未结 2 1308
情歌与酒
情歌与酒 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 <random>
     #include <time.h>
     #include <iostream>
        class enemy {
        private:
            std::random_device rd;
            int max_roll;
            typedef std::mt19937 MyRng;
            MyRng rng;
           std::uniform_int_distribution<int> 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.

    0 讨论(0)
  • 2021-01-14 07:19

    I think that your problem is that you are generating the same number each time.
    If I were you, I would consider using srand() in addition to rand() and time()

    Small explanation:
    If you want to generate a different random number each time, you have to use srand() function, in addition rand() function. Why? This is because rand generate the same pseudo-random number each time.

    srand() should receive something does changes each instant, such as the time or the PID (the id of the process)

    In my opinion, using time is very easy: (The following example using <ctime> could be a way of leading with this problem.)

    1) You need to create an auxiliar variable of type time_t, which is going to contain a seed depending on a time() function calling.

    2) time() function returns a pointer to time_t, so you pass the previous variable by reference to time() function in a later calling.

    3) After, you call srand() function, which receives a seed of type unsigned. So you can pass the auxiliar variable, doing a casting of type unsigned directly on the srand() calling when you pass the variable.

    #include <cstdlib>
    #include <ctime>
    
    time_t seconds;
    
    time(&seconds);
    
    // We create a seed that change each time that we run the code
    srand((unsigned)seconds);
    

    At last, whenever you want a pseudo-random number, you only have to call rand().

    Note that rand() can generate a different pseudo-random seed because of we called first to srand(), without calling srand() function previously, it will be the same seed.

    For example, we imagine that you want to throw a die some times, but you want that those throws are random, not the same each time: You could generate it by doing using the % math operation, which gets the rest of dividing the number by other.

    About ranges, if you want to include 0 or not:

    int die_throw = rand() % number; 
    

    You will get a pseudo-random number between 0 and number-1 If you don't want to include the zero, you could do something like add a number after getting the seed

    int die_throw = (rand() % number)+1; 
    // you generate a seed between 1 and number
    

    For more information, you can read:

    rand() function, including ranges srand() function

    I hope this answer will be helpful to you. Cheers

    0 讨论(0)
提交回复
热议问题