Re: 32-bit random numbers

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

Derek Parnell wrote:

> Juergen Luethje wrote:
>>
>> Hi all,
>>
>> for Euphoria's function rand(x), x may only be from 1 to the largest
>> positive value of type integer (#3FFFFFFF).
>>
>> I would like to have a function, that can take any unsigned 32-bit integer
>> -- i.e. in the interval [0,#FFFFFFFF] -- as argument. Any ideas?
>
> You could try something like this...
>
>  object vOldSeed vOldSeed = #69F5C10D
>  function rand32()
>     vOldSeed = vOldSeed * date()
>     vOldSeed = vOldSeed[1] + vOldSeed[2] + vOldSeed[3] + vOldSeed[4] +
>     vOldSeed[5] + vOldSeed[6]
>     vOldSeed += time() * (time() + 17)
>     vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
>     set_rand(vOldSeed)
>     return and_bits(rand(#3FFFFFFF), #FFFF) * #10000 +
>     and_bits(rand(#3FFFFFFF), #FFFF)
>  end function

Very nice, thanks Derek!
Do you think there is a way, that the function can take an argument,
that denotes the maximum returned value, just like the 'n' in Euphoria's
rand(n) does?

Maybe the last line of your function could be replaced with something
like this:

while 1 do
ret = and_bits(rand(#3FFFFFFF), #FFFF) * #10000 + and_bits(rand(#3FFFFFFF),
   #FFFF)
   if ret <= n then return ret end if
end while


But such a loop could sometimes take some time ... getlost

Regards,
   Juergen

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

Search



Quick Links

User menu

Not signed in.

Misc Menu