Article View: att.lang.c++
Article #7Re: random number generator
From: Pete Becker
Date: Sun, 01 Jun 2003 13:19
Date: Sun, 01 Jun 2003 13:19
64 lines
2865 bytes
2865 bytes
lovens weche wrote:
>
> Hi, for a lotto program I'm working on I want to code a random number
> generator *but* numbers generated must be always different.
>
> ex:
>
> 1st number genereated: 2
> 2nd number generated: 6
> 3rd number generated: 2 (WRONG!!! bad "period" as they say I think)
>
> ok so we can forget the rand() funtion (please don't tell me to code a
> routine that checks numbers generated and if they repeat re-draw a number,
> bad performance code...)
>
> ok here's the code I have to genereate the random numbers.. the variable a,
> c and m must be set by me the programmer.. hummm thats where my problems
> begin...
>
> --- the code (don't you love the C programming language?
> ;) ------------------
>
> a = 2 ^ 31;
This sets a to 2.
> c = 3;
> m = (2 ^ 31) -1;
This sets m to 1.
>
> // wait 1 millisecond
>
> VectCurrPtr[0] = (a * time() + c) % m;
>
> for (i = 1; i < NumElem; i++)
> VectCurrPtr[i] = (a * VectCurrPtr[i - 1] + c) % m;
>
This is a linear congruential generator. With the values you've given
above it will do a very bad job of producing random numbers. But even
with good values it will repeat. And it should. Think about it: if you
threw a die and in every six throws you got all six faces exactly once
you'd be very suspicious. And you'd have a game that was very easy to
beat.
You need a second step beyond the random number generator, to supply a
random sequence:
for (i = 0; i < NumElem; i++)
VectCurrPtr[i] = i;
for (i = NumElem; 1 < i; i--)
{
int j = rand() % (i - 1);
int temp = VectCurrPtr[j];
VectCurrPtr[j] = VectCurrPtr[i - 1];
VectCurrPtr[i - 1] = temp;
}
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Technical Details
Message-ID:
<3ED9FD62.2B70E8DD@acm.org>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!feed2.news.rcn.net!feed1.news.rcn.net!rcn!not-for-mailReferences:
<SikCa.1506$Z83.788@wagner.videotron.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 109fba,da54b5a763359bc3 X-Google-Attributes: gid109fba,public X-Google-Thread: 10d4ba,da54b5a763359bc3 X-Google-Attributes: gid10d4ba,public X-Google-Thread: 1014db,da54b5a763359bc3 X-Google-Attributes: gid1014db,public X-Google-ArrivalTime: 2003-06-01 06:20:01 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!feed2.news.rcn.net!feed1.news.rcn.net!rcn!not-for-mail From: Pete Becker <petebecker@acm.org> Newsgroups: att.lang.c++,comp.lang.c,comp.lang.c++ Subject: Re: random number generator Date: Sun, 01 Jun 2003 09:19:30 -0400 Organization: Dinkumware, Ltd Lines: 64 Message-ID: <3ED9FD62.2B70E8DD@acm.org> References: <SikCa.1506$Z83.788@wagner.videotron.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: UmFuZG9tSVZCO5QGgGpryUtzAWdLdPAPFZntc8fofsYD2KEas4cLE4j/MnrJqS8E X-Complaints-To: abuse@rcn.com NNTP-Posting-Date: 1 Jun 2003 13:19:32 GMT X-Mailer: Mozilla 4.79 [en] (Windows NT 5.0; U) X-Accept-Language: en