lua-users home
lua-l archive

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


> 
>   I'm looking at the code, and I think that seeding the random number
> generator is *not* something it should do---that should be left to the
> user
> to do.
> 

Just submitted an updated rockspec for version 0.2, which has this separated.

Also fixed the overflow by checking on the Lua side;
  local bitsize = 32
  function M.randomseed(seed)
    seed = math.floor(math.abs(seed))
    if seed >= (2^bitsize) then
      -- integer overflow, so reduce to prevent a bad seed
      seed = seed - math.floor(seed / 2^bitsize) * (2^bitsize)
    end
    if lua_version < 5.2 then
      -- 5.1 uses (incorrect) signed int
      math.randomseed(seed - 2^(bitsize-1))
    else
      -- 5.2 uses (correct) unsigned int
      math.randomseed(seed)
    end
    return seed
  end

regards
Thijs