random number generators

C

cesco

Hi I have a problem in using the using the boost library for generating
random numbers with a normal distribution.
I created the following files and classes:


#ifndef __NORMALDISTRIBUTION_H
#define __NORMALDISTRIBUTION_H

#include <boost/random.hpp>
using namespace std;

class NormalDistribution {
public:
typedef boost::rand48 EngineType;
typedef boost::normal_distribution<> DistributionType;
typedef boost::uint64_t SeedType;
typedef boost::variate_generator<EngineType, DistributionType>
VariateGeneratorType;

NormalDistribution(SeedType s, double mean, double std)
: mSeed(s)
{
DistributionType distribution(mean, std);
mDistribution = distribution;
mEngine.seed(mSeed);
mVariate(mEngine, mDistribution);
}
VariateGeneratorType::result_type mVariate();
VariateGeneratorType mVariate(EngineType, DistributionType);
private:
SeedType mSeed;
EngineType mEngine;
DistributionType mDistribution;
};

#endif //__NORMALDISTRIBUTION_H


//------------------
// ClassUsingNormalDistribution.h
//------------------

#ifndef __CLASSUSINGNORMALDISTRIBUTION_H
#define __CLASSUSINGNORMALDISTRIBUTION_H

#include "NormalDistribution.h"

class ClassUsingNormalDistribution {
public:
ClassUsingNormalDistribution();
~ClassUsingNormalDistribution() {}
void GenerateRndNumbers();
private:
NormalDistribution* mpNormDis;
};


#endif //__CLASSUSINGNORMALDISTRIBUTION_H


//------------------
// ClassUsingNormalDistribution.cpp
//------------------

#include<iostream>
using namespace std;
#include "ClassUsingNormalDistribution.h"

ClassUsingNormalDistribution::ClassUsingNormalDistribution()
{
mpNormDis = new NormalDistribution(0u, 0.0, 1.0);
}

void ClassUsingNormalDistribution::GenerateRndNumbers()
{
cout << mpNormDis->mVariate() << endl;
}


//------------------
// Main.cpp
//------------------

#include "ClassUsingNormalDistribution.h"

int main() {
ClassUsingNormalDistribution normDistObj;
for (int i = 0; i < 10; ++i)
normDistObj.GenerateRndNumbers();
}

---------------------------------------

and I get the following link errors:

1>ClassUsingNormalDistribution.obj : error LNK2019: unresolved external
symbol "public: class boost::variate_generator<class
boost::rand48,class boost::normal_distribution<double> > __thiscall
NormalDistribution::mVariate(class boost::rand48,class
boost::normal_distribution<double>)"
(?mVariate@NormalDistribution@@QAE?AV?$variate_generator@Vrand48@boost@@V?$normal_distribution@N@2@@boost@@Vrand48@3@V?$normal_distribution@N@3@@Z)
referenced in function "public: __thiscall
NormalDistribution::NormalDistribution(unsigned __int64,double,double)"
(??0NormalDistribution@@QAE@_KNN@Z)
1>ClassUsingNormalDistribution.obj : error LNK2019: unresolved external
symbol "public: double __thiscall NormalDistribution::mVariate(void)"
(?mVariate@NormalDistribution@@QAENXZ) referenced in function "public:
void __thiscall ClassUsingNormalDistribution::GenerateRndNumbers(void)"
(?GenerateRndNumbers@ClassUsingNormalDistribution@@QAEXXZ)
1>D:\3.9G\normal_distribution\Debug\normal_distribution.exe : fatal
error LNK1120: 2 unresolved externals

I don't know how to solve the problem. Can anyone help?

Thanks & regards
Cesco
 
P

Pete C

cesco said:
VariateGeneratorType::result_type mVariate();
VariateGeneratorType mVariate(EngineType, DistributionType);

You declared and used these two functions, but you never defined them
anywhere.
 
C

cesco

Thanks, but they are supposed to be defined in the library header
<boost/random.hpp> , I wouldn't know how to define them.
With the call
mVariate(mEngine, mDistribution);
I should be able to instantiate a random variate (called mVariate)
having the engine mEngine and the distribution mDistribution. Any
successive call to mVariate() should return a random value from that
distribution.

Here is the documentation library:
http://www.boost.org/libs/random/index.html

Any other suggestion?

Thanks again
 
P

Pete C

cesco said:
Thanks, but they are supposed to be defined in the library header
<boost/random.hpp> , I wouldn't know how to define them.

You have declared them as members of your class, which is wrong. The
variate_generator template gives you a class type that you can
instantiate (mVariate in your case), and then use as a funtion object.
There's an example at the very top of the page you linked to.
Try this, it will give you what you need - no need to modify your other
files (note how the mVariate member is a variable that behaves like a
function. And read up on functors, the boost libraries use them a
*lot*):

#ifndef __NORMALDISTRIBUTION_H
#define __NORMALDISTRIBUTION_H

#include <boost/random.hpp>
using namespace std;

class NormalDistribution {
public:
typedef boost::rand48 EngineType;
typedef boost::normal_distribution<> DistributionType;
typedef boost::uint64_t SeedType;
typedef boost::variate_generator<EngineType, DistributionType>
VariateGeneratorType;

NormalDistribution(SeedType s, double mean, double std)
: mVariate(EngineType(), DistributionType(mean, std))
{
}
VariateGeneratorType mVariate;

};

#endif //__NORMALDISTRIBUTION_H
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top