lua-users home
lua-l archive

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


Sometime on 3/17/2010, Geoff Leyland wrote:
....
I think the problem is that on some platforms the arguments to math.random get truncated to an int, even though srand takes an unsigned int as an argument. All your seeds are larger than an int.

Assuming that math.random and math.randomseed as red herrings and eliminating them from the test case, we simply have:

for _,s in ipairs{
3063121584, 2428144928, 3559301251, 4287790062, 2737803158, 2458923424,
    0x80000001, 0x80000000, 0x7fffffff,
    -2147483648, -2147483649, -2147483647,
} do
  print(("%%f=%.0f, %%u=%u, %%X=%X, %%d=%d"):format(s,s,s,s))
end

Which produces

%f=3063121584, %u=3063121584, %X=B69386B0, %d=-2147483648
%f=2428144928, %u=2428144928, %X=90BA8D20, %d=-2147483648
%f=3559301251, %u=3559301251, %X=D426A083, %d=-2147483648
%f=4287790062, %u=4287790062, %X=FF927BEE, %d=-2147483648
%f=2737803158, %u=2737803158, %X=A32F8F96, %d=-2147483648
%f=2458923424, %u=2458923424, %X=929031A0, %d=-2147483648
%f=2147483649, %u=2147483649, %X=80000001, %d=-2147483648
%f=2147483648, %u=2147483648, %X=80000000, %d=-2147483648
%f=2147483647, %u=2147483647, %X=7FFFFFFF, %d=2147483647
%f=-2147483648, %u=2147483648, %X=80000000, %d=-2147483648
%f=-2147483649, %u=2147483647, %X=7FFFFFFF, %d=-2147483648
%f=-2147483647, %u=2147483649, %X=80000001, %d=-2147483647

on my WinXP 32 bit box as well as an Ubuntu Karmic 32-bit box.
This makes it pretty clear that the conversion from double to int
is handling the overflow in an unexpected way. Knowing it will
saturate, having -2147483649 saturate to -2147483648 is
acceptable. However having 2147483649 saturate to the same value
is *surprising*.

Is this a Lua bug? Is there an easy fix? Is there a fix that
won't hurt performance and still give an acceptable answer?

At least its clear that math.random and math.randomseed are not
the actual issue here.

Ross Berteig                               Ross@CheshireEng.com
Cheshire Engineering Corp.           http://www.CheshireEng.com/