Article View: att.lang.c++
Article #10Re: random number generator
From: Shane Beasley
Date: Sun, 01 Jun 2003 14:13
Date: Sun, 01 Jun 2003 14:13
94 lines
4481 bytes
4481 bytes
"lovens weche" <fw1803@videotron.ca> wrote in message news:<SikCa.1506$Z83.788@wagner.videotron.net>...
> 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)
No, that's not the period. The period is the point at which the random
sequence generated by rand() repeats itself. If this were the period
of rand(), then further calls to rand() would generate the sequence
(6, 2, 6, 2, 6, ...), and that's not very random at all.
rand() models a uniform distribution, meaning that one number is as
likely to be returned as any other, regardless of whether you received
it before. What you want is a combination (as opposed to permutation)
algorithm. You have N things (e.g., numbered balls in a bin) and you
want to select K of them at random. rand() can be made to do that:
#include <stdio.h>
#include <stdlib.h>
/* return random n such that lo <= n < hi (implies lo < hi) */
int randrange (int lo, int hi) {
return rand() / (RAND_MAX / (hi - lo));
}
/* swap *p and *q */
void int_swap (int *p, int *q) { int n = *p; *p = *q; *q = n; }
/* shuffle the contents of the range [p, q) */
void int_random_shuffle (int *p, int *q) {
while (p != q) { int_swap(p, p + randrange(0, q - p)); ++p; }
}
int main () {
int balls[60], i;
srand(time(0));
for (i = 0; i < 60; ++i) balls[i] = i + 1;
int_random_shuffle(balls, balls + 60);
for (i = 0; i < 6; ++i) printf("%d\n", balls[i]);
}
NB: If this were C++, I'd've used std::random_shuffle. However, out of
my own courtesy for comp.lang.c, I didn't.
> 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...)
Nothing wrong with rand(); you just don't know how to use it. And I
doubt you need any kind of serious performance for a lottery game.
(I'd be more concerned with the small but real chance that the program
might never end.)
> the code (don't you love the C programming language? ;)
Not particularly. And why did you post this to C++ groups?
> // 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 the lagged Fibonacci algorithm, a random number generator with
a uniform distribution. It's probably the algorithm that your rand()
uses. In other words, you probably just rewrote rand(), and for no
good reason.
> so if you eant look at the rules, then my code and tell me if my numbers
> will repeat. AND DON'T TELL ME TO CODE ADDITIONAL CODE TO CHECK IF THEY
> REAPEAT!!! already done that :(
Your code implements a uniform random number generator. Of course it's
going to repeat.
BTW: Why did you ask here? Running it should have told you that.
> ps: if you can send reply directly to my emial address that would be great:
C++ FAQ Lite, FAQ 5.4
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4>
Do not say, "Please respond by e-mail because I don't normally read
this newsgroup". If you don't have enough time for the newsgroup,
don't expect the newsgroup to have enough time for you.
- Shane
Technical Details
Message-ID:
<2f2d78e1.0306010613.71adae94@posting.google.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!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: 10d4ba,da54b5a763359bc3 X-Google-Attributes: gid10d4ba,public X-Google-Thread: 1014db,da54b5a763359bc3 X-Google-Attributes: gid1014db,public X-Google-Thread: 109fba,da54b5a763359bc3 X-Google-Attributes: gid109fba,public X-Google-ArrivalTime: 2003-06-01 07:13:57 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!postnews1.google.com!not-for-mail From: sbeasley@cs.uic.edu (Shane Beasley) Newsgroups: att.lang.c++,comp.lang.c,comp.lang.c++ Subject: Re: random number generator Date: 1 Jun 2003 07:13:56 -0700 Organization: http://groups.google.com/ Lines: 94 Message-ID: <2f2d78e1.0306010613.71adae94@posting.google.com> References: <SikCa.1506$Z83.788@wagner.videotron.net> NNTP-Posting-Host: 12.249.234.146 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1054476837 11285 127.0.0.1 (1 Jun 2003 14:13:57 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 1 Jun 2003 14:13:57 GMT