473,804 Members | 3,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 8734
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.publi c.dotnet.langua ges.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.sewro te in message
news:11******** *************@o 5g2000hsb.googl egroups.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 objektorientier ter Datenverarbeitu ng
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1357
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 evaluating (trying to evaluate) Team Foundation Server and VS 2005 Team System. I'm running the Dec CTP release. To be honest, it's been a struggle getting all this lot talking to each other but I've managed to finally do it.
13
3618
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 distribution random number generator and then using the function in another part of my program in order to use the value. This function is called for many iterations. The problem is, through each run I am getting the exact same number generated...
2
1431
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 with search them. Here is the code i've been so far using System; using System.Collections.Generic;
4
1646
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 it work under the hood. Failing that I have three specific questions: 1 Is the core PRNG using integer arithmetic or floating point arithmetic? The presence of Sample() makes me suspect that it is floating point.
12
3233
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 without success. for example if the number is 123 456 : i would like to have some random strings like theese : (12 * 10 000) + (345 * 10) + (6*1) or (123*1 000)+(4*100)+(5*10)+(6*1) etc...
9
5763
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 call it Express for simplicity). I have, to try to simplify things, put the exact same DB on two systems, one running MSDE and one running Express. Both have 2 Ghz processors (one Intel, one AMD), both have a decent amount of RAM (Intel system...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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
10571
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...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9143
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7615
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
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.