473,474 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

random number generators

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::ClassUsingNormalDist ribution()
{
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_gen erator@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(v oid)"
(?GenerateRndNumbers@ClassUsingNormalDistribution@ @QAEXXZ)
1>D:\3.9G\normal_distribution\Debug\normal_distrib ution.exe : fatal
error LNK1120: 2 unresolved externals

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

Thanks & regards
Cesco

Feb 3 '06 #1
4 3843
cesco wrote:
VariateGeneratorType::result_type mVariate();
VariateGeneratorType mVariate(EngineType, DistributionType);


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

Feb 3 '06 #2
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

Feb 3 '06 #3
cesco wrote:
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

Feb 3 '06 #4
Great! You are completely right:-)
It worked.

Sincerely thanks a lot
Francesco

Feb 3 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
7
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
23
by: MConly | last post by:
Can you tell me what happens inside CPU when I rand() ? Where can I find the true rand function implemented ? I have heard that rand() in C/C++ is n't a good one but why it isn't a good one, are...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
15
by: felixnielsen | last post by:
This is something i have done before and i know its pretty simple, however i cant remember how it works exactly, and i need it i kinda hurry, so if someone would be so nice to drop a random number...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
11
by: TreatmentPlant | last post by:
I need to generate a few thousand true random numbers using C++. I have some code now that does alright, but when you plot the results on a graph, you notice patterns, so the numbers are not...
0
by: urkel | last post by:
Hello everybody, I have these errors on C program that may be because of random number definition. I use an SSH to compile C like in Linux. May be you have idea whether the errors are due to the...
7
by: BillG | last post by:
Hi, Does anyone know of a site or have code for a function that will generate a random string or random number? I need one where I can tell it what type of value I need and where I can set the...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.