www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/09/11/05:02:35

From: amit <halffoat AT bigfoot DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random Numbers
Date: Thu, 11 Sep 1997 09:02:52 +0300
Organization: none
Lines: 29
Message-ID: <3417979C.6286@bigfoot.com>
References: <3413736A DOT 5E38 AT voyageur DOT ca>
NNTP-Posting-Host: abarak-desk.iil.intel.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

J.E. wrote:
> 
> I was just wondering:  How do you create a random number in Djgpp?  I
> tried setting a value for RAND_MAX and then going c = rand(), but that
> didn't work (it said that the value for RAND_MAX, which was 2, was
> invalid.  Any help would be appreciated.
> 
> Jordan Ellis   <cellis AT voyageur DOT ca>

RAND_MAX is already defined in "stdlib.h" and you may not change it
(well, you may, but this is considered bad practice, and it probably
won't change the range of numbers 'rand()' will produce) RAND_MAX
is there to let you know what to expect, and not to control 'rand()' ..

one simple way to obtain (integer) random number between 0 and arbitrary
number M is to use the remainder operator (%) for instance

  b = rand()%(M+1) ;

will assign a random number between 0 and M into 'b'.
note that M needs to be considerebly smaller than RAND_MAX in order
for 'b' to be 'reasonably' evenly distributed    

 i.e: prob(b==0) = prob(b==1) =  ...  = prob(b==M) = 1/(M+1)


-amit

(there should only be single 'f' in my addrress)

- Raw text -


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