By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,704 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Apr 4 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Apr 4, 12:32 pm, Juha Nieminen <nos...@thanks.invalidwrote:
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.

Apr 4 '07 #2

P: n/a
On 4 Apr, 11:32, Juha Nieminen <nos...@thanks.invalidwrote:
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.

--
Erik Wikström

Apr 4 '07 #3

P: n/a
"Erik Wikström" <er****@student.chalmers.sewrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...

On 4 Apr, 11:32, Juha Nieminen <nos...@thanks.invalidwrote:
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
Apr 4 '07 #4

P: n/a
Erik Wikström wrote:
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.
Apr 4 '07 #5

P: n/a
Juha Nieminen wrote:
Erik Wikström wrote:
>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.
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
Apr 4 '07 #6

P: n/a
Juha Nieminen wrote:
Erik Wikström wrote:
>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.
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)
Apr 4 '07 #7

P: n/a
On Apr 4, 12:50 pm, "Erik Wikström" <eri...@student.chalmers.se>
wrote:
On 4 Apr, 11:32, Juha Nieminen <nos...@thanks.invalidwrote:
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.
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 5 '07 #8

P: n/a
"Juha Nieminen" <no****@thanks.invalidwrote in message
news:46***********************@news.song.fi...
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).
Apr 6 '07 #9

P: n/a
* Jim Langston:
// On my system RAND_MAX is 37267 (7 bits)
I count that as 15 bits.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 6 '07 #10

P: n/a
"Alf P. Steinbach" <al***@start.nowrote in message
news:57*************@mid.individual.net...
>* Jim Langston:
> // On my system RAND_MAX is 37267 (7 bits)

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.
Apr 6 '07 #11

P: n/a
On Apr 6, 12:34 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
// 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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 6 '07 #12

P: n/a
Comment at bottom

"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
On Apr 6, 12:34 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
// 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.
Apr 6 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.