Re: 32-bit random numbers

new topic     » goto parent     » topic index » view thread      » older message » newer message

Tommy Carlier wrote:
> How to create a 32-bit random integer between 1 and n (n = 32-bit integer
> atom):
> 
> function rand32(atom n)
>     if n <= #3FFFFFFF then return rand(n)
>     else return rand(n - #3FFFFFFF) + rand(#3FFFFFFF) - 1
> end function
> 
> PS: if you leave out the -1 at the end, the number 1 will never be chosen.

That will work, but keep in mind that the probability distribution
of the sum of two random numbers is not flat. For example,
if you roll two dice (1-6), the sum is far more likely to 
be 7 than 12. So what would I do? I'm not sure, but I guess you
could take two random 16-bit numbers and concatenate them into
a 32-bit number. I'd still have some slight worries though,
since the two 16-bit numbers would be consecutive numbers
from the same random number generator. Are they truly independent?
Of course not, but then rand() is really just giving you 
"pseudo-random" numbers anyway.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu