Newsgroups: comp.security.misc,sci.crypt,alt.security
Path: news.io.com!uunet!europa.chnt.gtegsc.com!cantaloupe.srv.cs.cmu.edu!das-
+ news2.harvard.edu!oitnews.harvard.edu!news.sesqui.net!uuneo.neosoft.com!
+ nmtigw!peter
From: peter@nmti.com (Peter da Silva)
Subject: Re: Random Number Generators
Message-ID:
Sender: peter@nmti.com (peter da silva)
Organization: Network/development platform support, NMTI
References: <3qfbkc$pnp@sol.ctr.
+ columbia.edu>
Date: Fri, 2 Jun 1995 21:47:26 GMT
Lines: 45
Xref: news.io.com comp.security.misc:17475 sci.crypt:38136 alt.security:25253
In article , Adam Back wrote:
> Peter da Silva writes:
> | Seth Robertson writes:
> | > Something like: ((double)seed) / ((double)(1<<27)) * 100000000.0
> | But this will still produce a situation where some numbers are more
> | likely than others, since you're still mapping between the same sets of
> | integers.
> Here's a method which I think solves this problem. [...]
> Say we were using rand() producing numbers r,
> r in [0..2**32)
> And we want to transform this into a random number in the range
> r' in [0..n)
> Then you could calculate an integer scale factor S:
> S = int( 2**32 / n )
> ie integer division of 2**32 / n
> And take for use this algorithm to generate random numbers in [0..n)
> 1) x = rand();
> 2) x' = int( x / S );
> 3) if x' > n goto 1)
Or what I usually do:
X = S * n
1) x = rand();
2) if x >= X goto 1
3) x' = int ( x / S ) or x % n
This moves the division outside the loop.
--
Peter da Silva `-_-'
Network Management Technology Incorporated 'U`
1601 Industrial Blvd. Sugar Land, TX 77478 USA
+1 713 274 5180 "Har du kramat din varg idag?"