randomlua

Lua math.random returns erratic values


I was wondering why I had invalid data for a script, then I tried to test math.random since it seemed to be coming from it's return values. This is what I got from the Lua console :

> 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

I am a little confused about the results I'm getting. Someone can clarify?


Solution

  • 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).