Using Boost's random number generator in my own class, how?

J

John C

Hi, I am trying to include the generation of random numbers in my c++ class.
However I don't quite know how to incorporate it.

To start with, I managed to get random numbers going via the following...

============
#include <boost/random/linear_congruential.hpp>
#include <boost/random/uniform_real.hpp>
#include <boost/random/variate_generator.hpp>

int main ( void )
{
boost::minstd_rand generator(42u);
boost::uniform_real<> uni_dist(0,1);
boost::variate_generator<boost::minstd_rand, boost::uniform_real<> >
uni(generator, uni_dist);

for ( int i = 0 i <= 100; i++ )
cout << uni() << endl;
}
===============

However I want to incorporate the above into a class which has member
functions that use random numbers. how do i do it? For example here is a
simple class that defines a circle with an origin and radius (which is
generated randomly). How do I incorporate boost into this class? I have
tried setting:

===========

class circle
{
public:
double x;
double y;
double r;

boost::variate_generator<boost::minstd_rand, boost::uniform_real<> > myGen;
}

class :: class ( double a, double b)
{
x=a;
y=b;
myGen uni = myGen(0,1);
r= uni();
}

=========

... but i get a compiler error:

c:\Documents and Settings\a1\My Documents\Visual Studio
Projects\qw\source\C.cpp(17): error C2512:
'boost::variate_generator<Engine,Distribution>' : no appropriate default
constructor available
with
[
Engine=circle::base_generator_type &,
Distribution=boost::uniform_real<>
]

Anyone know how to do it? I know it should be simple. I know that this error
is occuring due to my definition of myGen in the header file. Thanks for any
help!

cheers,
John
 
R

Rob Williscroft

John C wrote in in comp.lang.c++:
class circle
{
public:
double x;
double y;
double r;

boost::variate_generator<boost::minstd_rand, boost::uniform_real<> >
myGen;

circle( double a, double b ); // declaration
}

class :: class ( double a, double b)

/** defenition
*/
circle::circle( double a, double b ) : myGen( 0, 1 )

The bit after the : is called an initializer list, you can initialize
members and base classes here, if you don't they will be default
initialized.

When a class like boost::variate_generator< ... > that lacks a default
constructor, is a member or base class it *must* be initialized in this
list.
{
x=a;
y=b;
myGen uni = myGen(0,1);

myGen isn't a type name its a member variable.
r= uni();
}

Please read the FAQ <http://www.parashift.com/c++-faq-lite/>, also
consider geting a (good) book on C++, you can learn C++ by typing
random nonsense into your editor and seeing if it compiles, but its
going to take you a long long time.

HTH.

Rob.
 

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