Lua math.random returns 不稳定的值

Lua math.random returns erratic values

我想知道为什么我的脚本数据无效,然后我尝试测试 math.random,因为它似乎来自它的 return 值。这是我从 Lua 控制台得到的:

> return  math.random(0.8, 1.2);
0.8
> return  math.random(0.8, 1.2);
0.8
> return  math.random(0.8, 1.2);
0.8
> return  math.random(0.8, 1.2);
1.8
> return  math.random(0.8, 1.2);
0.8
> return  math.random(0.8, 1.2);
1.8
> return  math.random(0.8, 1.2);
0.8
> return  math.random(0.8, 1.2);
1.8
> return  math.random(0.8, 1.2);
0.8

我对得到的结果有点困惑。有人可以澄清一下吗?

http://lua-users.org/wiki/MathLibraryTutorial

upper and lower must be integer. In other case Lua casts upper into an integer, sometimes giving math.floor(upper) and others math.ceil(upper), with unexpected results (the same for lower).

会说 Yanick Rochon

upper and lower must be integer. In other case Lua casts upper into an integer, sometimes giving math.floor(upper) and others math.ceil(upper), with unexpected results (the same for lower).

因此:

return math.random(0.8, 1.2); 0.8

文档参考说:

math.random ([m [, n]]) This function is an interface to the simple pseudo-random generator function rand provided by ANSI C. (No guarantees can be given for its statistical properties.) When called without arguments, returns a uniform pseudo-random real number in the range [0,1). When called with an integer number m, math.random returns a uniform pseudo-random integer in the range [1, m]. When called with two integer numbers m and n, math.random returns a uniform pseudo-random integer in the range [m, n].

字体:http://goo.gl/eJvLup