468,525 Members | 2,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,525 developers. It's quick & easy.

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

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

Jul 22 '05 #1
1 5659
John C wrote in news:41******@dnews.tpgi.com.au 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Joe | last post: by
7 posts views Thread by Chris Gordon-Smith | last post: by
16 posts views Thread by Leon | last post: by
104 posts views Thread by fieldfallow | last post: by
16 posts views Thread by Markus Dehmann | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.