473,770 Members | 3,912 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_congruen tial.hpp>
#include <boost/random/uniform_real.hp p>
#include <boost/random/variate_generat or.hpp>

int main ( void )
{
boost::minstd_r and 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\Visua l Studio
Projects\qw\sou rce\C.cpp(17): error C2512:
'boost::variate _generator<Engi ne,Distribution >' : no appropriate default
constructor available
with
[
Engine=circle:: base_generator_ type &,
Distribution=bo ost::uniform_re al<>
]

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 6274
John C wrote in news:41******@d news.tpgi.com.a u 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.c om/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
10761
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 consisting of 6 numbers - with a total of 50 groups of numbers. A well-known girl that some publishing companies use to provide introductory level textbooks to various Junior Colleges in the U.S., not surprisngly, asks for this same exact...
3
7381
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 have utilised the following random number source code http://www.agner.org/random/ What I have found is that there is a problem with seeding. The code generates a seed based on time(0). I have found that I need to increment
7
3247
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 library, which is based on GTK. Depending on which libraries I link in, I can have either a GTK1 or GTK2 user interface. I'm finding that the simulation behaves differently depending on whether I link with GTK1 or GTK2.
16
12079
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 numbers using asp.net? I know about the random namespace within .net, but I need a reference to some code that do the similar stated function above. Plus If you have any coding practice ideas for the above defined project please share them.
104
5187
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 better way than to fill an array of range 0... RAND_MAX with pre-computed primes and using the output of rand() to index into it to extract a random prime.
89
6076
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
16
8794
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 sequence on every program call if the program is called several times in a second! Isn't there a better method to seed the random numbers? Probably based on milliseconds instead of seconds? And integrating the process ID or current amount of free...
6
11751
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 that will randomly generate 10 unique random numbers. Your job is to write a program that produces random permutations of the numbers 1 to 10. “Permutation” is a mathematical name for an arrangement. For example, there are six permutations of the...
16
2084
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
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
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 we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.