lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Here's a PRNG someone suggested to me on Stack Exchange:

-- rough adaptation of Park-Miller generator
function srandom(seedobj, fVal1, fVal2)
local B = 2^31
local seed = seedobj[1]
local k = mod(floor(seed/127773), B)
seed = mod(16807 * (seed - mod(k * 127773, B)), B)
seed = seed - mod(2836 * k, B)
if (seed < 0) then
seed = mod( seed + B - 1, B )
end
seedobj[1] = seed
local temp_rand = seed/(B - 1)
if not fVal1 then
return temp_rand
elseif not fVal2 then
return floor(temp_rand * fVal1) + 1
else
return floor(fVal1 + 0.5 + temp_rand * (fVal2 - fVal1))
end
end

It suffers from the same problem. But maybe it's easier to modify?


Mike