From: buers AT gmx DOT de (Dieter Buerssner) Newsgroups: comp.os.msdos.djgpp Subject: Re: Odp: Random numbers Date: 29 May 2000 11:47:50 GMT Lines: 85 Message-ID: <8gtsle.3vvrb0l.0@buerssner-17104.user.cis.dfn.de> References: <392CB951 DOT 34B432E6 AT chemistry DOT uq DOT edu DOT au> <018801bfc877$2a916ba0$0100a8c0 AT ivan> NNTP-Posting-Host: pec-106-63.tnt6.s2.uunet.de (149.225.106.63) Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: fu-berlin.de 959600870 2106670 149.225.106.63 (16 [17104]) X-Posting-Agent: Hamster/1.3.13.0 User-Agent: Xnews/03.02.04 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Grzegorz Patoła wrote: >Chris Miller wrote: >> If RAND_MAX is set to 100, then a call to rand() should return an >> integer between 0 and 100. > >You can try sth like this: > >int lotto; >lotto = rand() % 100; You shouldn't use this method. Eli already has pointed out the DJGPP FAQ in this thread, where you can find an explaination, why this method should be avoided. The following small program shows the failure of this method. Instead of 100, 32 is used. To find the failure, quite many calls to DJGPP rand() are needed. Many other rand() implementations would give up much earlier. The program calculates many random numbers between 0 and 32 (exclusive), and counts, how many times it found each number. The method you suggested and the method suggested in the FAQ are used. #include #include #include #include #define NTRY (1UL<<26) #define RANGE 32 #define EXPECTED ((double)NTRY/RANGE) int main(void) { static unsigned long count[RANGE], count2[RANGE]; unsigned long i; int j; double s, s2, d, t, t2; srand(time(NULL)); for (i = 0; i < NTRY; i++) { count[rand()%RANGE]++; /* %-Method */ count2[(unsigned)((double)rand()*RANGE/(RAND_MAX+1.0))]++; /* FAQ */ } printf("Expecting on average E=%f\n", EXPECTED); printf(" r fnd(mod) (E-f)^2/E sum fnd(div) (E-f)^2/E sum\n" s = s2 = 0.0; for (j=0; j