bennyxqg / lua-alchemy

Automatically exported from code.google.com/p/lua-alchemy
0 stars 0 forks source link

math.random() may return values outside of [0, 1) #127

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
math.randomseed(12345)

for i = 1, 944845 do
  local a, b, c = 
    math.random(),
    math.random(2 ^ 29),
    math.random()

  if a < 0 or a >= 1 then
    error(i .. " bad a "..a)
  end

  if b < 1 or b > (2 ^ 29) or b % 1 ~= 0 then
    error(i .. " bad b "..b)
  end

  if c < 0 or c >= 1 then
    error(i .. " bad c "..c)
  end
end

--> builtin://test/math.lua:17: 19637 bad a 1.0000005871999

Original issue reported on code.google.com by aglad...@gmail.com on 20 Sep 2009 at 6:20

GoogleCodeExporter commented 9 years ago

Original comment by aglad...@gmail.com on 20 Sep 2009 at 6:44

GoogleCodeExporter commented 9 years ago
Workaround:

Override math.random as follows:

local old_math_random = math.random
math.random = function(a, b)
  local v = old_math_random(a, b)
  if a == nil and b == nil then
    v = v % 1
  end
  return v
end

Original comment by aglad...@gmail.com on 21 Sep 2009 at 5:16