如何在lua中生成随机浮点数?

时间:2012-07-18 18:33:46

标签: random lua

我需要在Lua中生成随机 float 。它必须是> 1,所以math.random()不是解决方案。

我该怎么做?

3 个答案:

答案 0 :(得分:10)

这应该生成1(包括)和100(不包括)

之间的随机浮点数
math.random() + math.random(1, 99)

答案 1 :(得分:7)

您也可以使用类似的内容来获取lowergreater之间的数字

function randomFloat(lower, greater)
    return lower + math.random()  * (greater - lower);
end

答案 2 :(得分:2)

只是为了好玩而发帖,但你可以使用没有参数的math.random()来做到这一点:P

print(math.floor((math.random()*100)+0.5))