I'm a C++ beginner coming from a Java and C# background. I'm trying to use the same default_random_engine and normal_distribution at every creation of a new object. Before I was using a new default_random_engine with a new seed and a new normal_distribution in every constructor. I think that way the normal_distribution doesn't work correctly.
Old code ->
my_object.cpp:
default_random_engine generator;
MyObject() {
double mean = 1.0;
double std = 0.5;
normal_distribution<double> distribution(mean, std);
QTime time = QTime::currentTime();
uint milli = (time.hour() * 60 * 60 * 1000) + (time.minute() * 60 * 1000) + (time.second() * 1000) + time.msec();
generator.seed(milli);
myValue = distribution(generator);
}
This compiled and the values for myValue were randomly distributed. I just think they didn't match the normal distribution, because I always created a new default_random_engine and normal_distribution and used a new seed.
My new code ->
main.h:
class Main
{
public:
static default_random_engine generator;
static normal_distribution<double> distribution;
};
main.cpp:
default_random_engine generator;
normal_distribution<double> distribution;
int main(int argc, char *argv[]) {
double mean = 1.0;
double std = 0.5;
distribution(mean, std);
QTime time = QTime::currentTime();
uint milli = (time.hour() * 60 * 60 * 1000) + (time.minute() * 60 * 1000) + (time.second() * 1000) + time.msec();
generator.seed(milli);
...
}
my_object.cpp:
default_random_engine generator;
normal_distribution<double> distribution;
MyObject() {
myValue = distribution(generator);
}
But now I get 10 errors on compile time:
error C2228: left of '.min' must have class/struct/union
error C2780: '_Rty std::_Nrand(_Engine &,long double,_Rty)' : expects 3 arguments - 2 provided
error C2780: '_Rty std::_Nrand(_Engine &,double,_Rty)' : expects 3 arguments - 2 provided
...
What am I doing wrong? Why am I getting the errors? And am I correct that earlier my normal distribution/random generation wasn't correct? Will I be able to produce the wanted normal distribution this way?
Aucun commentaire:
Enregistrer un commentaire