有具有步长选项的math.random函数吗?

一个自定义函数,可返回一个带有步长选项的随机数,就像在for循环中一样。

例如:

for i=1,10,**2** do
   print(i)
end
点赞
用户869951
用户869951

你是指这个吗:

function randomWithStep(first, last, stepSize)
    local maxSteps = math.floor((last-first)/step)
    return first + stepSize * math.random(0, maxSteps)
end

这个功能与 math.random(first, last) 相同,只不过生成的值相隔为"stepSize"。需要注意的是,最高随机数可能不是"last",这取决于(last-first) 是否为 stepSize 的倍数。

2014-02-15 22:08:52