lua-users home
lua-l archive

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


On Mon, Jun 18, 2018 at 11:33 PM Luiz Henrique de Figueiredo wrote:
Lua 5.4.0 (work2) is now available for testing at
        http://www.lua.org/work/lua-5.4.0-work2.tar.gz

math.randomseed(0); for j=1,10 do print(math.random()) end

Unfortunately, this sequence of random numbers depends on number of binary digits in the mantissa of a float datatype Lua was built with (despite of the same seed!).
To fix it, "I2d(Rand64 x)" function should use the highest FIGS bits of x (instead of the lowest FIGS bits).
I'm talking about this code in "lua-5.4.0-work2/src/lmathlib.c":

#define maskFIG   (~(~(Rand64)1 << (FIGS - 1)))  /* use FIGS bits */
#define shiftFIG  (l_mathop(0.5) / ((Rand64)1 << (FIGS - 1)))  /* 2^(-FIGS) */

static lua_Number I2d (Rand64 x) {
  return (lua_Number)(x & maskFIG) * shiftFIG;
}