lua-users home
lua-l archive

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


I found a function that works:

http://love2d.org/forums/viewtopic.php?p=133941#p133941

function mul16(a, b)
local a_lo, b_lo = mod(a, 2^8), mod(b, 2^8)
local a_hi, b_hi = a - a_lo, b - b_lo
return mod(a_lo * b_lo + mod(a_lo * b_hi, 2^16) + mod(a_hi * b_lo,
2^16) + mod(a_hi * b_hi, 2^16), 2^16)
end

function lcg(s, r)
local temp = {}
function temp:random(a, b)
local y = mod(mul16(self.a, self.x) + self.c, self.m)
self.x = y
if not a then
return y / 65536
elseif not b then
if a == 0 then
return y
else
return 1 + mod(y, a)
end
else
return a + mod(y, b - a + 1)
end
end
function temp:randomseed(s)
if not s then
s = seed()
end
self.x = mod(s, 2147483648)
end
-- 'Numerical Recipes' parameters
temp.a = 26125
temp.c = 62303
temp.m = 65536
temp:randomseed(s)
return temp
end

local R = lcg(0974)
local rand_table = {}
for i = 1, 10000 do
local new_value = R:random()
rand_table[i] = new_value
if (i > 1) then
for j = 1, i - 1 do
local old_value = rand_table[j]
if (new_value == old_value) then
print("\ti = " .. i .. "\n\tj = " .. j .. "\n\tnew_value = " ..
new_value .. "\n\told_value = " .. old_value)
break
end
end
end
end


I don't know if the statistical properties are any good, but the
returned values don't repeat themselves at least.

(Also, why does gmail remove the indentation from my code??)


Mike

On Tue, Sep 10, 2013 at 1:19 PM, Doug Currie <doug.currie@gmail.com> wrote:
>
> On Sep 10, 2013, at 11:52 AM, Michael Horvath <mikh2161@gmail.com> wrote:
>
>> Yeah I tried  local B = 2^23 and  local B = 2^15, but it just made
>> matters worse.
>
> You may have an easier time with a Lagged Fibonacci Generator.
>
> http://en.wikipedia.org/wiki/Lagged_Fibonacci_generator
>
> Use a modulus of 2^23 so the addition is not truncated.
>
> e
>
>
>
>