delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/22/21:54:05

Sender: nate AT cartsys DOT com
Message-ID: <36A7A2FA.D99D4A17@cartsys.com>
Date: Thu, 21 Jan 1999 13:58:18 -0800
From: Nate Eldredge <nate AT cartsys DOT com>
X-Mailer: Mozilla 4.08 [en] (X11; I; Linux 2.0.36 i586)
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: Re: rand() function
References: <36A72C2A DOT 3DAF8CFE AT cryogen DOT com>
Reply-To: djgpp AT delorie DOT com

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

You don't want to.  The better solution is to use your knowledge that
the number varies from 0 to RAND_MAX to scale the numbers
appropriately.  So, if you want a number between 0 and n:

(int)(rand()/(double)RAND_MAX)*n

`rand()%n' also works but tends to yield less randomness.

-- 

Nate Eldredge
nate AT cartsys DOT com

- Raw text -


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