lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 11/7/2012 11:33 AM, Rena wrote:
Just noticed on Lua 5.1, on a 32-bit machine, math.random(0,
0x7FFFFFFF) always returns a negative number:
return math.random(0, 0x7FFFFFFF)
-1649760493

The unfortunate effect of:

lua_pushnumber(L, floor(r*(u-l+1))+l);
    /* int between `l' and `u' */

A bit inconvenient there...

0x7FFFFFFE has no such issue. 0x80000000 and up give an error which is
a bit nonsensical, but I at least understand why:
return math.random(0, 0x80000000)
stdin:1: bad argument #2 to 'random' (interval is empty)

--
Cheers,
Kein-Hong Man (esq.)
Kuala Lumpur, Malaysia