简体   繁体   中英

How to make a member function of a class in C++ generate a different random number each time it is called?

I have a class which also includes a random number engine and its distribution:

#include <iostream>
#include <cmath>
#include <random>
#include <chrono>

class C
{ 
  public:
      typedef std::mt19937_64 engine;
      typedef std::uniform_real_distribution<double> distribution;
      .
      .
      .

  protected:
      engine rng;
      distribution dist;
      void func();
      .
      .
      .
};

Since the constructor is called only once, I put the seed in it:

C::C()
{   .
    .
    .  
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    distribution dist(0.0, pow(10,12));
    engine rng(seed);
}

The following member function is supposed to generate a random number and is going to be called lots of times in a single run of the program:

void C::func()
{    .
     .
     .
     double randNum = floor(dist(rng));
     std::cout << randNum << std::endl;      
     .
     .
     .
}

However, each time it generates the number 0 as the random number. It seems that dist(rng) is not doing its job.

I really need to find the problem and correct the output. I would appreciate any help.

You define your dist and eng variables in the class

protected:
  engine rng;
  distribution dist; // Here
  void func();

And in the constructor you define another with the same name

C::C()
{   .
    .
    .  
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    distribution dist(0.0, pow(10,12)); // Here
    engine rng(seed);
}

I assume you want the latter to be an assignment, not a new variable. Then you will get the numbers you want.

C::C()
{   .
    .
    .  
    unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
    dist = distribution(0.0, pow(10,12)); // Here
    rng = engine(seed);
}

Or with initialization

C() : dist(0.0, pow(10, 12)),
  rng(std::chrono::system_clock::now().time_since_epoch().count())

Sami is close, but you should really be initialising these members:

C::C()
  : dist(0.0, pow(10, 12))
  , rng(std::chrono::system_clock::now().time_since_epoch().count())
{}

In the constructor, you're creating a local variable dist that hides the class member, while the class member gets default-initialized. A default-initialized uniform_real_distribution generates values in the range [0,1), which floor will always turn into 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM