How to use System::Random in Visual C++ 2005 Express?

J

Juha Nieminen

I'm trying to use a random number generator and rand() is just not
up to the task (RAND_MAX is way too small).

I searched in the help system and found the System::Random class
which seems to be just perfect.
However, for some odd reason the help files simply refuse to tell
what should be #included in order to use the class. I can't find this
info anywhere! Even the example program given in the help lacks this
information (and thus of course doesn't compile).
I have tried google-searching, to no avail. I can't find this info
anywhere.
 
C

Cupu

I'm trying to use a random number generator and rand() is just not
up to the task (RAND_MAX is way too small).

I searched in the help system and found the System::Random class
which seems to be just perfect.
However, for some odd reason the help files simply refuse to tell
what should be #included in order to use the class. I can't find this
info anywhere! Even the example program given in the help lacks this
information (and thus of course doesn't compile).
I have tried google-searching, to no avail. I can't find this info
anywhere.

That's pretty much because System::Random is in .NET land (e.g. C++/
CLI) thus not really c++ or related to this group (you could try
microsoft.public.dotnet.languages.vc). Of course in case I'm wrong and
someone defined some Random class in a System namespace .. post the
example and we'll know for sure.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

I'm trying to use a random number generator and rand() is just not
up to the task (RAND_MAX is way too small).

I searched in the help system and found the System::Random class
which seems to be just perfect.
However, for some odd reason the help files simply refuse to tell
what should be #included in order to use the class. I can't find this
info anywhere! Even the example program given in the help lacks this
information (and thus of course doesn't compile).
I have tried google-searching, to no avail. I can't find this info
anywhere.

System::Random is part of the .Net framework and you must create a
managed project to use it. Notice however that using such things means
that your code is no longer pure C++, but rather a vendor-specific
"extension".

There are ways to make do with rand(), you can call rand() twice and
multiply the numbers, run it many times and add the results until you
get a result big enough, multiply the result of rand() with some
factor to scale it, and so on. Depending on what you plan to use the
random number for some approaches are better than others.
 
P

P.J. Plauger

I'm trying to use a random number generator and rand() is just not
up to the task (RAND_MAX is way too small).

I searched in the help system and found the System::Random class
which seems to be just perfect.
However, for some odd reason the help files simply refuse to tell
what should be #included in order to use the class. I can't find this
info anywhere! Even the example program given in the help lacks this
information (and thus of course doesn't compile).
I have tried google-searching, to no avail. I can't find this info
anywhere.

System::Random is part of the .Net framework and you must create a
managed project to use it. Notice however that using such things means
that your code is no longer pure C++, but rather a vendor-specific
"extension".

There are ways to make do with rand(), you can call rand() twice and
multiply the numbers, run it many times and add the results until you
get a result big enough, multiply the result of rand() with some
factor to scale it, and so on. Depending on what you plan to use the
random number for some approaches are better than others.

[pjp] Or, if you need higher quality random distributions, see the
random package in Boost. It's also a part of TR1, which means you
get it as part of our Compleat Libraries.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
J

Juha Nieminen

Erik said:
There are ways to make do with rand(), you can call rand() twice and
multiply the numbers, run it many times and add the results until you
get a result big enough, multiply the result of rand() with some
factor to scale it, and so on.

But how many values will the standard rand() give before it starts
repeating? I certainly need more than 32768 values.
 
K

Kai-Uwe Bux

Juha said:
But how many values will the standard rand() give before it starts
repeating? I certainly need more than 32768 values.

That's a quality of implementation issue. However, I doubt that if RAND_MAX
is that small it would also give the period.

If you need to be certain about the quality of the random number generator,
you should not use std::rand() but a known random number generator of your
choice. There are random number generators is Boost and in tr1.


Best

Kai-Uwe Bux
 
P

Pete Becker

Juha said:
But how many values will the standard rand() give before it starts
repeating? I certainly need more than 32768 values.

It's not specified. But note that RAND_MAX tells you the maximum value
that rand() will return. The length of a cycle (i.e. how many values it
will give before it starts repeating) is another matter. A truly bad
implementation could simply provide alternating values of 0 and
RAND_MAX, for a cycle length of 2. Another one could run through all
possible permutations of the values from 0 to RAND_MAX, for a cycle
length of RAND_MAX factorial. In practice, the length will usually be
less than RAND_MAX.

When you combine values from multiple calls to a random number
generator, the cycle length of the result depends on how you combine the
values. It's unpredictable without more information.

For good generators with well-defined characteristics (rand doesn't
qualify here: it's largely up to the implementation to determine what it
does), look at TR1's random number generators (they're also in Boost,
although that may be a bit different from what ended up in TR1). There
are several linear congruential generators with known good properties;
there's a mersenne twister; and a couple of others that are a bit more
specialized. See chapter 13 of my book, "The Standard C++ Library
Extensions," for more details.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
J

James Kanze

System::Random is part of the .Net framework and you must create a
managed project to use it. Notice however that using such things means
that your code is no longer pure C++, but rather a vendor-specific
"extension".
There are ways to make do with rand(), you can call rand() twice and
multiply the numbers, run it many times and add the results until you
get a result big enough, multiply the result of rand() with some
factor to scale it, and so on. Depending on what you plan to use the
random number for some approaches are better than others.

Several others have proposed Boost, which is doubtlessly the
best solution. I'd just like to point out that the suggestions
above do not work; the resulting distribution is anything but
linear.
 
J

Jim Langston

Juha Nieminen said:
I'm trying to use a random number generator and rand() is just not
up to the task (RAND_MAX is way too small).

I searched in the help system and found the System::Random class
which seems to be just perfect.
However, for some odd reason the help files simply refuse to tell
what should be #included in order to use the class. I can't find this
info anywhere! Even the example program given in the help lacks this
information (and thus of course doesn't compile).
I have tried google-searching, to no avail. I can't find this info
anywhere.

Here. A random number generator that will (on my system) produce a random
number from 0 to MAX_INT

#include <iostream>
#include <string>
#include <ctime>

unsigned int BigRand( )
{
// On my system RAND_MAX is 37267 (7 bits)
int RandNum; // I have 4 bytes on my system or 32 bits
RandNum = rand(); // Low 7 bits.
RandNum = RandNum | rand() << 7; // next 7
RandNum = RandNum | rand() << 14; // next 7
RandNum = RandNum | rand() << 21; // next 7
RandNum = RandNum | ( rand() & 0xF ) << 25; // last 4 bits
return RandNum;
}

int main()
{
srand(clock());

std::cout << RAND_MAX << "\n";

for ( int i = 0; i < 100; ++i )
std::cout << BigRand( ) << "\n";

std::string wait;
std::getline( std::cin, wait );
}

Modify it to taste (sizeof int and RAND_MAX on your system).
 
J

Jim Langston

Alf P. Steinbach said:
* Jim Langston:

I count that as 15 bits.

*blink* *blink*

unsigned int BigRand( )
{
// On my system RAND_MAX is 37267 (15 bits)
int RandNum; // I have 4 bytes on my system or 32 bits
RandNum = rand(); // Low 15 bits.
RandNum = RandNum | rand() << 15; // next 15
RandNum = RandNum | ( rand() & 0x3 ) << 30; // last 2
return RandNum;
}

Sorry, had a brain fart there.
 
J

James Kanze

// On my system RAND_MAX is 37267 (7 bits)

Just in passing, a RAND_MAX value of the form 2^n-1 is probably
a sign of a very poor random number generator.
int RandNum; // I have 4 bytes on my system or 32 bits
RandNum = rand(); // Low 7 bits.
RandNum = RandNum | rand() << 7; // next 7
RandNum = RandNum | rand() << 14; // next 7
RandNum = RandNum | rand() << 21; // next 7
RandNum = RandNum | ( rand() & 0xF ) << 25; // last 4 bits

And note that this effectively reduces the period of your
generator by four. In addition, if it is a linear congruent
generator (the most frequent), you almost certainly won't see
all possible values. (If the period of the generator is less
than INT_MAX, you certainly won't.)

Unless you really, really know what you are doing, use Boost.
 
J

Jim Langston

Comment at bottom

// On my system RAND_MAX is 37267 (7 bits)

Just in passing, a RAND_MAX value of the form 2^n-1 is probably
a sign of a very poor random number generator.
int RandNum; // I have 4 bytes on my system or 32 bits
RandNum = rand(); // Low 7 bits.
RandNum = RandNum | rand() << 7; // next 7
RandNum = RandNum | rand() << 14; // next 7
RandNum = RandNum | rand() << 21; // next 7
RandNum = RandNum | ( rand() & 0xF ) << 25; // last 4 bits

And note that this effectively reduces the period of your
generator by four. In addition, if it is a linear congruent
generator (the most frequent), you almost certainly won't see
all possible values. (If the period of the generator is less
than INT_MAX, you certainly won't.)

Unless you really, really know what you are doing, use Boost.

=====

I agree that rand() is not truely random and there are lots of problems with
it (can become predictable, repeatable, missing values, etc..) but if the
only problem with it is the range, then something like I posted would be
sufficient. If, however, you are having other problems with rand() then
yes, use some better pseudo random number generator.
 

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

No members online now.

Forum statistics

Threads
474,301
Messages
2,571,549
Members
48,295
Latest member
JayKillian
Top