1

我无法访问 Lua 4.0 中的 random()(不要问),所以我需要推出自己的随机数生成器。或者更确切地说,我必须推出另一个,因为我几年前实施的那个现在让我失望了。即我注意到重复值很糟糕。

我可以使用用 Lua 编写的任何建议或示例吗?仅供参考,这是我迄今为止一直在使用的:

seedobja = 1103515245
seedobjc = 12345
seedobjm = 4294967295 --0x100000000

function srandom(seedobj, fVal1, fVal2)
    seedobj[1] = mod(seedobj[1] * seedobja + seedobjc, seedobjm)
    local temp_rand = seedobj[1] / (seedobjm - 1)
    if (fVal2) then
        return floor(fVal1 + 0.5 + temp_rand * (fVal2 - fVal1))
    elseif (fVal1) then
        return floor(temp_rand * fVal1) + 1
    else
        return temp_rand
    end
end

[编辑]

后来编辑删除。

4

3 回答 3

3

这是另一个尝试(始终是 Lua 5.1 代码),使用 Knuth 的减法生成器的 C 改编(当时不是线性同余)。根据 Knuth 的说法,它应该与 FP 算术(甚至单精度)一起使用。

local mod = math.fmod
local floor = math.floor
local abs = math.abs

local B =  4000000

-- rough adaptation of Knuth float generator
function srandom( seedobj, fVal1, fVal2 )
    local ma = seedobj.ma
    local seed = seedobj.seed
    local mj, mk
    if seed < 0 or not ma then
        ma = {}
        seedobj.ma = ma
        mj = abs( 1618033 - abs( seed ) )
        mj = mod( mj, B )
        ma[55] = mj
        mk = 1
        for i = 1, 54 do
            local ii = mod( 21 * i,  55 )
            ma[ii] = mk
            mk = mj - mk
            if mk < 0 then mk = mk + B end
            mj = ma[ii]
        end
        for k = 1, 4 do
            for i = 1, 55 do
                ma[i] = ma[i] - ma[ 1 + mod( i + 30,  55) ]
                if ma[i] < 0 then ma[i] = ma[i] + B end
            end
        end
        seedobj.inext = 0
        seedobj.inextp = 31
        seedobj.seed = 1
    end -- if
    local inext = seedobj.inext
    local inextp = seedobj.inextp
    inext = inext + 1
    if inext == 56 then inext = 1 end
    seedobj.inext = inext
    inextp = inextp + 1
    if inextp == 56 then inextp = 1 end
    seedobj.inextp = inextp
    mj = ma[ inext ] - ma[ inextp ]
    if mj < 0 then mj = mj + B end
    ma[ inext ] = mj
    local temp_rand = mj / B
    if fVal2 then
        return floor( fVal1 + 0.5 + temp_rand * ( fVal2 - fVal1 ) )
    elseif fVal1 then
        return floor( temp_rand * fVal1 ) + 1
    else
        return temp_rand
    end
end

-- test

-- Note: seedobj must be a table with a field named `seed`;
-- this field must be negative; after the first number has
-- been generated, the seedobj table will be populated with
-- additional state needed to generate numbers; changing its
-- `seed` field to a negative number will reinitialize the
-- generator and start a new pseudorandom sequence.
local seedobj = { seed = -232343 }
for i = 1, 100 do
    print( srandom( seedobj, 100, 1000 ) )
end
于 2013-09-10T17:35:27.543 回答
2

我没有安装 Lua 4.0,也从未使用过它,因此下面的代码可能需要一些调整。

这适用于 Lua 5.1。它是对 Park 和 Miller 生成器(用 C 语言编写,使用 32 位整数)实现的粗略改编。我试图更接近 4.0 语法(我从你的代码片段中猜到了)。测试它,看看它的周期是否适合您的需要。原始版本有一个约2e9,但转换为浮点算术可能会破坏某些东西(这些生成器是微妙的东西)。

local mod = math.fmod
local floor = math.floor

local B = 2^31
-- rough adaptation of Park-Miller generator
function srandom( seedobj, fVal1, fVal2 )
    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 fVal2 then
        return floor( fVal1 + 0.5 + temp_rand * ( fVal2 - fVal1 ) )
    elseif fVal1 then
        return floor( temp_rand * fVal1 ) + 1
    else
        return temp_rand
    end
end

-- test
local seedobj = { 2 }   -- first element is the seed and must not be 0
for i = 1, 100 do
    print( srandom( seedobj, 100, 1000 ) )
end
于 2013-09-10T09:57:07.363 回答
2

我在这里找到了解决方案:

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

我不知道统计属性,但即使经过数千次迭代,该函数也不会返回重复值。无论如何感谢大家的帮助!

于 2013-09-11T13:59:49.120 回答