mirror of
https://github.com/veden/Rampant.git
synced 2024-12-30 21:19:46 +02:00
64 lines
1.4 KiB
Lua
64 lines
1.4 KiB
Lua
local mathUtils = {}
|
|
|
|
-- imports
|
|
|
|
local constants = require("Constants")
|
|
|
|
-- constants
|
|
|
|
local TICKS_A_MINUTE = constants.TICKS_A_MINUTE
|
|
|
|
local INTERVAL_LOGIC = constants.INTERVAL_LOGIC
|
|
|
|
-- imported functions
|
|
|
|
local mMax = math.max
|
|
local mSqrt = math.sqrt
|
|
local mLog10 = math.log10
|
|
|
|
function mathUtils.roundToNearest(number, multiple)
|
|
local num = number + (multiple * 0.5)
|
|
return num - (num % multiple)
|
|
end
|
|
|
|
function mathUtils.randomTickEvent(tick, low, high)
|
|
local minutesToTick = mMax(high * math.random(), low)
|
|
local nextTick = mathUtils.roundToNearest(TICKS_A_MINUTE * minutesToTick, INTERVAL_LOGIC)
|
|
return tick + nextTick
|
|
end
|
|
|
|
--[[
|
|
Used for gaussian random numbers
|
|
--]]
|
|
local function marsagliaPolarMethod(rg)
|
|
local iid1
|
|
local iid2
|
|
local q
|
|
repeat
|
|
if rg then
|
|
iid1 = 2 * rg() + -1
|
|
iid2 = 2 * rg() + -1
|
|
else
|
|
iid1 = 2 * math.random() + -1
|
|
iid2 = 2 * math.random() + -1
|
|
end
|
|
q = (iid1 * iid1) + (iid2 * iid2)
|
|
until (q ~= 0) and (q < 1)
|
|
local s = mSqrt((-2 * mLog10(q)) / q)
|
|
return iid1 * s
|
|
end
|
|
|
|
function mathUtils.gaussianRandom(mean, std_dev, rg)
|
|
return mean + (marsagliaPolarMethod(rg) * std_dev)
|
|
end
|
|
|
|
function mathUtils.gaussianRandomRange(mean, std_dev, min, max, rg)
|
|
local q
|
|
repeat
|
|
q = mathUtils.gaussianRandom(mean, std_dev, rg)
|
|
until (q >= min) and (q <= max)
|
|
return q
|
|
end
|
|
|
|
return mathUtils
|