lua-users home
lua-l archive

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


Here's the code again.

--Linear Congruential Generator
function lcg(s, r)
    local temp = {}
    function temp:random(a, b)
        local y = mod(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
    if r then
        --from Numerical Recipes
        if r == 'nr' then
            temp.a = 1664525
            temp.c = 1013904223
            temp.m = 65536
        --from MVC
        elseif r == 'mvc' then
            temp.a = 214013
            temp.c = 2531011
            temp.m = 65536
        end
    --from Ansi C
    else
        temp.a = 1103515245
        temp.c = 12345
        temp.m = 65536
    end
    temp:randomseed(s)
    return temp
end

local R = lcg(0)
print("wutwut =" .. R:random())
print("wutwut =" .. R:random())
print("wutwut =" .. R:random())

On Mon, Sep 9, 2013 at 10:04 PM, Geoff Leyland
<geoff_leyland@fastmail.fm> wrote:
> On 10/09/2013, at 1:40 PM, Michael Horvath <mikh2161@gmail.com> wrote:
>
>> I copied what you typed but still get the bad results. Is it because I
>> am working in Lua 4.0 not 5.0?
>
> Whoops, sorry, missed that.  I tested it with Lua 4.0.1 and it worked as expected.  Either there's something funny with the version of Lua in your game, or you've got a typo.  In any case, unless you post the code, it's hard to help.
>
>