I am trying to mimic Minecraft slime chunk generation algorithm, but it seems that the random algorithm used in Lua Random class is different from Java.
I tried searching the source code of Java Random in a search engine, but the output of the Lua version looks different
Here is my current Lua code
local JavaRandom = {}
JavaRandom.__index = JavaRandom
function JavaRandom.new(Seed: number)
local RandomObject = {
Seed = bit32.band(Seed, bit32.lshift(1, 48) - 1),
Multiplier = 0x5DEECE66D,
Addend = 0xB,
Mask = bit32.lshift(1, 48) - 1
}
setmetatable(RandomObject, JavaRandom)
return RandomObject
end
function JavaRandom:Next(Bits)
self.Seed = bit32.band(self.Seed * self.Multiplier + self.Addend, self.Mask)
return bit32.rshift(self.Seed, 48 - Bits)
end
function JavaRandom:NextInteger(Num: number?)
if Num == nil then
return self:Next(32)
else
if Num <= 0 then
error("Num must be positive")
end
if bit32.band(Num, -Num) == Num then
return bit32.rshift(bit32.band(Num * self:Next(31), 0x7FFFFFFF), 31)
end
local Bits, Val
repeat
Bits = self:Next(31)
Val = bit32.band(Bits, 0x7FFFFFFF) % Num
until Bits - Val + (Num - 1) >= 0
return Val
end
end
return JavaRandom
My English is not very good, so I use Google Translate