473,236 Members | 1,307 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,236 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 6224
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++) {
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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...

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.