X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-Trace-PostClient-IP: 68.147.225.57 From: Brian Inglis Newsgroups: comp.os.msdos.djgpp Subject: Re: random() : What am I doing wrong? Organization: Systematic Software Message-ID: <60s3e31ouck81u9ti1njogqcoj1a5tfta3@4ax.com> References: <200709051534 DOT l85FYdil032635 AT envy DOT delorie DOT com> X-Newsreader: Forte Agent 1.93/32.576 English (American) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 30 Date: Sat, 08 Sep 2007 00:45:58 GMT NNTP-Posting-Host: 64.59.135.176 X-Complaints-To: abuse AT shaw DOT ca X-Trace: pd7urf2no 1189212358 64.59.135.176 (Fri, 07 Sep 2007 18:45:58 MDT) NNTP-Posting-Date: Fri, 07 Sep 2007 18:45:58 MDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com On Wed, 5 Sep 2007 11:34:39 -0400 in comp.os.msdos.djgpp, DJ Delorie wrote: > >> Well, okay - random() doesn't anything like guarantee >> equidistribution. But that said, wouldn't scaling the result set >> at least not give you anything *worse* than what you started with? > >If you do the math, you see that there are 47 numbers left over when >you mod MAX_INT % 50. That means the last three numbers (47, 48, 49) >are slightly less likely to be chosen. The difference in probability? >1 in 42,949,672 (0.9999999767169356). > >So, for a range this small, it really doesn't matter, and if it *does* >matter, you probably already know more about random number generation >than any of us here. > >However, integer modulus is a LOT cheaper than floating point >division. IIRC the best distributed approach is to take a sufficient number of bits b around the middle of the random integer, shift right to give you a set 0..2^b-1 and if the number is bigger than you can use, try again with a new random integer. -- Thanks. Take care, Brian Inglis Calgary, Alberta, Canada Brian DOT Inglis AT CSi DOT com (Brian[dot]Inglis{at}SystematicSW[dot]ab[dot]ca) fake address use address above to reply