www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/12/01/03:12:08

Date: Sun, 1 Dec 1996 09:50:58 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Paul Dixon <p DOT dixon AT ic DOT ac DOT uk>
Cc: djgpp AT delorie DOT com
Subject: Re: Random numbers ...
In-Reply-To: <199611301124.LAA09903@mailhost.sm.ic.ac.uk>
Message-Id: <Pine.SUN.3.91.961201094839.12293T-100000@is>
Mime-Version: 1.0

On Sat, 30 Nov 1996, Paul Dixon wrote:

> >    while ( tolower( getchar( ) ) != 'q' )
> >    {
> >        r = random( ) % 501;	   /* 0 to 500 */
> >        printf( "%d", r );
> >    }
> 
> I hate to say this, but it is NOT generally advisable to use MODULUS to 
> force a random number into a specified range if you really care about the 
> quality of the random numbers.

In general, yes, but not with `random'.  Its algorithm makes sure that  
all the bits generated by random() are usable.  For example,

          random()&01

should produce a random binary value.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019