mirror of
https://github.com/wxd-gaming/test-all.git
synced 2026-05-08 06:28:01 +08:00
61 lines
1.3 KiB
Lua
61 lines
1.3 KiB
Lua
--- 种子随机数
|
|
--- Generated by EmmyLua(https://github.com/EmmyLua)
|
|
--- Created by 無心道(15388152619).
|
|
--- DateTime: 2025/2/27 09:43
|
|
|
|
|
|
SeedRandom = {}
|
|
SeedRandom.__index = SeedRandom
|
|
|
|
--- 创建一个事件监听容器
|
|
function SeedRandom.new(seed)
|
|
local var = setmetatable({}, SeedRandom)
|
|
var.oldSeed = seed or os.time() ^ 999999999
|
|
var.addSeed = 0
|
|
return var
|
|
end
|
|
|
|
function SeedRandom:refreshSeed()
|
|
self.addSeed = self.addSeed + 1
|
|
self.oldSeed = (self.oldSeed * 9301 + self.addSeed) & 233280;
|
|
end
|
|
|
|
function SeedRandom:nextInt(min, max)
|
|
if (max <= min) then
|
|
return min;
|
|
end
|
|
|
|
local tmp = max - min;
|
|
local next00 = self:next();
|
|
|
|
return math.floor(min + math.floor(tmp * next00));
|
|
end
|
|
|
|
function SeedRandom:nextFloat(min, max)
|
|
if (max <= min) then
|
|
return min;
|
|
end
|
|
local tmp = max - min;
|
|
local next00 = self:next();
|
|
return min + tmp * next00;
|
|
end
|
|
|
|
function SeedRandom:next()
|
|
self:refreshSeed();
|
|
local v = self.oldSeed / 233280.0;
|
|
if (v < 0) then
|
|
v = 0;
|
|
end
|
|
if (v > 1) then
|
|
v = 1;
|
|
end
|
|
return v;
|
|
end
|
|
|
|
function SeedRandom.onInit()
|
|
print("lua SeedRandom.onInit")
|
|
local seedRandom = SeedRandom.new(9527003456)
|
|
for i = 1, 10 do
|
|
print(i, seedRandom:nextInt(1, 1000))
|
|
end
|
|
end |