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 if      -- smile
> end function
>
> PS: if you leave out the -1 at the end, the number 1 will never be chosen.

Hi, this function can not take a big 32-bit integer atom as argument.
If n > 2*#3FFFFFFF, then (n - #3FFFFFFF) > #3FFFFFFF, that means the
argument to the first call of rand() will be too big, and Euphoria
aborts with a syntax error.

Also, I want the rand32() function to behave like Euphoria's built-in
rand() function. That means, the returned values must have a uniform
distribution. Are you sure, that the sum
      rand(n - #3FFFFFFF) + rand(#3FFFFFFF)
is uniformly distributed?

Sorry, I don't have a statistical test for this at hand ATM.

Regards,
   Juergen

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

Search



Quick Links

User menu

Not signed in.

Misc Menu