mirror of
https://github.com/Refactorio/RedMew.git
synced 2025-01-03 22:52:13 +02:00
added extra functions
This commit is contained in:
parent
199bce4b00
commit
99bd04578e
@ -15,19 +15,18 @@ function Random.new(seed1, seed2)
|
||||
return random
|
||||
end
|
||||
|
||||
local function get_uint(self)
|
||||
self.z = 36969 * bit32.band(self.z, 65535) + bit32.rshift(self.z, 16)
|
||||
self.w = 18000 * bit32.band(self.w, 65535) + bit32.rshift(self.w, 16)
|
||||
local ret = bit32.lshift(self.z, 16) + self.w
|
||||
return bit32.band(ret, 4294967295)
|
||||
end
|
||||
|
||||
local e = 1 / (2 ^ 32 + 2)
|
||||
|
||||
-- return float (0, 1) exclusive
|
||||
function Random.next(self)
|
||||
local u = get_uint(self)
|
||||
return (u + 1.0) * e
|
||||
local z, w = self.z, self.w
|
||||
z = 36969 * bit32.band(z, 65535) + bit32.rshift(z, 16)
|
||||
w = 18000 * bit32.band(w, 65535) + bit32.rshift(w, 16)
|
||||
self.z, self.w = z, w
|
||||
|
||||
local ret = bit32.lshift(z, 16) + w
|
||||
ret = bit32.band(ret, 4294967295)
|
||||
return (ret + 1.0) * e
|
||||
end
|
||||
|
||||
-- returns int [min, max] inclusive
|
||||
@ -38,4 +37,20 @@ function Random.next_int(self, min, max)
|
||||
return math.floor(u)
|
||||
end
|
||||
|
||||
function Random.next_from_point(x, y)
|
||||
x = 36969 * bit32.band(x, 65535) + bit32.rshift(x, 16)
|
||||
y = 18000 * bit32.band(y, 65535) + bit32.rshift(y, 16)
|
||||
|
||||
local ret = bit32.lshift(x, 16) + y
|
||||
ret = bit32.band(ret, 4294967295)
|
||||
return (ret + 1.0) * e
|
||||
end
|
||||
|
||||
function Random.next_int_from_point(x, y, min, max)
|
||||
local u = Random.next_from_point(x, y)
|
||||
|
||||
u = u * (max - min + 1) + min
|
||||
return math.floor(u)
|
||||
end
|
||||
|
||||
return Random
|
||||
|
Loading…
Reference in New Issue
Block a user