Re: 32-bit random numbers
- Posted by "Juergen Luethje" <j.lue at gmx.de> Jul 05, 2004
- 579 views
Me wrote: > Derek Parnell wrote: > > [snipped old text] > >> 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 > > Cool. Thanks, Derek! I made a synthesis of yours and Tommy's code.
function rand32 (atom n) -- Produces a pseudo-random integer between 1 and n atom x integer hiWord, loWord if n <= #3FFFFFFF then return rand(n) else hiWord = rand(#10000) - 1 loWord = rand(#10000) - 1 x = hiWord*#10000 + loWord return remainder(x, floor(n)) + 1 end if end function
Derek, do you think using set_rand() is essential here? Regards, Juergen