www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/22/22:24:08

Date: Thu, 21 Jan 1999 19:27:05 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: FireFoX McLouD <firefox AT cryogen DOT com>
cc: djgpp AT delorie DOT com
Subject: Re: rand() function
In-Reply-To: <36A72C2A.3DAF8CFE@cryogen.com>
Message-ID: <Pine.SUN.3.91.990121192340.19490E-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Thu, 21 Jan 1999, FireFoX McLouD wrote:

> How can I redefine the RAND_MAX parameter to use rand() with low
> numbers?

You can't.  Even if you do redefine RAND_MAX, the behavior of `rand' 
won't change, since `rand' was compiled with the original definition of 
RAND_MAX.

The simplest way to get random integers from 0 to N is to say this:

	rand()%(N+1)

But beware: for small values of N you will get not-so-random numbers, 
because the low-order bits of what `rand' produces are known to be not so 
random.

There are better ways, if the last caveat bothers you.  One of them is to 
use `random' instead of `rand'.  For others, you will have to find a good 
book on random numbers, it's too complicated to tell it here.

- Raw text -


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