473,325 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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 6236
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
16
by: Leon | last post by:
I need a program that generate 5 non-duplicates random number between 1-10 as string values store in an array. Do anybody know of any good books or websites that explain how to generator random...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
16
by: Markus Dehmann | last post by:
According to several C++ tutorials, calling srand like this to initialize the random number generator seems to be standard: srand((unsigned)time(0)); But it leads to the same random number...
6
by: badcrusher10 | last post by:
Hello. I'm having trouble figuring out what to do and how to do.. could someone explain to me what I need to do in order to work? THIS IS WHAT I NEED TO DO: Professor Snoop wants a program...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.