Re: 32-bit random numbers

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

Juergen Luethje wrote:
> 
> 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:
> 
> <font color="#330033"></font>
> <font color="#0000FF">while </font><font color="#330033">1 </font><font
> color="#0000FF">do</font>
> <font color="#330033">   ret = </font><font
> color="#FF00FF">and_bits</font><font color="#330033">(</font><font
> color="#FF00FF">rand</font><font color="#993333">(</font><font
> color="#330033">#3FFFFFFF</font><font color="#993333">)</font><font
> color="#330033">, #FFFF) * #10000 + </font><font
> color="#FF00FF">and_bits</font><font color="#330033">(</font><font
> color="#FF00FF">rand</font><font color="#993333">(</font><font
> color="#330033">#3FFFFFFF</font><font color="#993333">)</font><font
> color="#330033">, #FFFF)</font>
> <font color="#0000FF">   if </font><font color="#330033">ret <= n </font><font
> color="#0000FF">then return </font><font color="#330033">ret </font><font
> color="#0000FF">end if</font>
> <font color="#0000FF">end while</font>
> <font color="#330033"></font>
> 
> But such a loop could sometimes take some time ... getlost
> 
> Regards,
>    Juergen

Maybe this will be useful...

-- rand32.e
-- Produces a pseudo-random integer between 1 and N

include machine.e
object vOldSeed vOldSeed = #69F5C10D

function rand32(atom N)
    integer a, b
    sequence d
    atom X

    d = date()
    d = vOldSeed * d
    vOldSeed = d[1] + d[2] + d[3] + d[4] + d[5] + d[6]
    vOldSeed += time() * (time() + 17)
    vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
    set_rand(vOldSeed)
    a = rand(#3FFFFFFF)
    vOldSeed += d[4] + d[5] + d[6]
    vOldSeed += (time() + 3.1427) * (time() + 619)
    vOldSeed = floor(remainder(vOldSeed * (vOldSeed - 3), #3FFFFFFF))
    set_rand(vOldSeed)
    b = rand(#3FFFFFFF)
    X =  and_bits(a, #FFFF) * #10000 + and_bits(b, #FFFF)
    return floor(X - floor(X/N)*N) + 1

end function
constant debug = find("DEBUG:RAND32", command_line())
if debug then
for i = 1 to 32 do
    printf(1, "%4d ", rand32(2))
end for
end if

-- 
Derek Parnell
Melbourne, Australia

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

Search



Quick Links

User menu

Not signed in.

Misc Menu