• Subject: Re: PRNG for Lua 4.0?
• From: Thijs Schreijer <thijs@...>
• Date: Tue, 10 Sep 2013 15:45:25 +0000

See my previous message on truncation and Dirks reply regaring size.

Looks to me
local B = 2^31
Gets to big. Maybe experiment by reducing that value.

Thijs

-------- Oorspronkelijk bericht --------
Van: Michael Horvath <mikh2161@gmail.com>
Datum:
Aan: Lua mailing list <lua-l@lists.lua.org>
Onderwerp: Re: PRNG for Lua 4.0?

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