delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/05/05/23:14:28

From: eheft AT dnaco DOT net (Eric Heft)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Random generator?
Date: 2 May 1997 20:11:10 GMT
Organization: The Dayton Network Access Company (DNACo)
Lines: 32
Message-ID: <5kdhou$o9r$1@pike.dnaco.net>
References: <2 DOT 2 DOT 32 DOT 19970429193847 DOT 006a0ec4 AT gate72> <33665CFB DOT C26 AT cs DOT com>
NNTP-Posting-Host: kirk.dnaco.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On getting random numbers :

If you do a histogram of rand() % 100 or random() % 100 you will 
find that its not uniform. If you image that rand return a uniform 
number from 0-31 and you % 10 you map 

[ rand val ] -> (val % 10)
[0,10,20,30] -> 0
[1,11,21,31] -> 1
[2,12,22]    -> 2
[3,13,23]    -> 3
[4,14,24]    -> 4
  . . .         .
[9,19,29]    -> 9


Depending on what your doing it may be close enough not to matter. 

Somthing like this gets rid of this problem. 

int rnd(int lo,int hi)
{
   int rng = hi - lo + 1;
   return ( lo + rng * rand() / MAX_INT );
}


I only spent a couple days trying to find why a modeler I was 
writing always favored low number 8^)



- Raw text -


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