local ApsRandom = BaseClass("ApsRandom")
local MULTIPLIER = 25214903917
local ADDEND = 11
local MASK_31 = 2147483647
local MASK_48 = 281474976710655
local __init = function(self, seed)
  self.seed = 0
  if seed then
    self:SetSeed(seed)
  end
end
local __delete = function(self)
  self.seed = nil
end
local InitialScramble = function(seed)
  return (seed ~ MULTIPLIER) & MASK_48
end
local SetSeed = function(self, seed)
  self.seed = InitialScramble(seed)
end
local GetSeed = function(self)
  return self.seed
end
local Next = function(self, bits)
  self.seed = self.seed * MULTIPLIER + ADDEND & MASK_48
  return self.seed >> 48 - bits & MASK_31
end
local NextInt = function(self, bound)
  local r = self:Next(31)
  local m = bound - 1
  if bound & m == 0 then
    r = bound * r >> 31 & MASK_31
  else
    local u = r
    r = u % bound
    while u - r + m < 0 do
      u = self:Next(31)
      r = u % bound
    end
  end
  return r
end
ApsRandom.__init = __init
ApsRandom.__delete = __delete
ApsRandom.SetSeed = SetSeed
ApsRandom.GetSeed = GetSeed
ApsRandom.Next = Next
ApsRandom.NextInt = NextInt
return ApsRandom
