473,385 Members | 1,673 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,385 software developers and data experts.

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

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

Similar topics

1
by: Mairead O' Donovan | last post by:
From: =?Utf-8?B?c3dn?= <swg@discussions.microsoft.com> Subject: VS2005 Team System - Whidbey Date: Fri, 1 Apr 2005 01:15:03 -0800 Newsgroups: microsoft.public.dotnet.datatools I'm currently...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
2
by: Default User | last post by:
Hi too all, I'm trying to write a simple program which will find all the folders in the c drive, but i'm having problems I've managed to search the root of c drive folders but having problems...
4
by: rossum | last post by:
I am looking for a source for the internal details of the System.Random pseudo random number generator. Something reasonably technical please, not how to use it (which I know) but more how does...
12
by: Pascal | last post by:
hello and soory for my english here is the query :"how to split a string in a random way" I try my first shot in vb 2005 express and would like to split a number in several pieces in a random way...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.