在Mac上生成的随机数是随机的,而在Windows上则不是。

我遇到了一个有趣的问题。下面是一个函数的代码段,它接受两个整数并返回两个整数(x y坐标)。我在循环中生成了5个对象。在Mac上,它会返回两个与其他数不同的随机数。但在PC上,它每次返回相同的两个确切数字,尽管我每次都进行了种子生成。有任何想法吗?

local randomSeed = 60

randomCoord = function(bufferX,bufferY)
    -- randomCoord
    -- int, int - get a buffer from the edge
    -- returns two random coordinates that are within background Plane space
    print( randomSeed )
    math.randomseed(randomSeed + os.time())
    randomSeed = randomSeed + os.time()
    local x = math.random(backgroundBounds.xMin + bufferX,backgroundBounds.xMax - bufferX)
    local y = math.random(backgroundBounds.yMin + bufferY,backgroundBounds.yMax - bufferY)
    print('random x '..x..'  random y '..y)
    return x, y
end

backgroundBounds只是一个整数表格(即背景框的大小)。

点赞
用户88888888
用户88888888

大多数情况下,在大多数语言中,随机数工具只需要被种子化一次。重新种子化可能会产生意外的结果,例如返回相同的数字序列。

2014-04-05 07:13:19