2017-11-15 14:41:34 +02:00
|
|
|
--Author: Valansch
|
|
|
|
|
2017-11-15 12:53:49 +02:00
|
|
|
local function generate_pmf_chart(l)
|
|
|
|
chart = {[0] = math.exp(-l)}
|
|
|
|
for k=1,(l*2 + 1) do
|
|
|
|
chart[k] = (chart[k - 1] * l / k)
|
|
|
|
end
|
|
|
|
return chart
|
|
|
|
end
|
|
|
|
|
|
|
|
local function generate_poisson_set(l, n) --n defines the resolution
|
|
|
|
local chart = generate_pmf_chart(l)
|
|
|
|
local set = {}
|
|
|
|
for x,y in pairs(chart) do
|
|
|
|
local m = math.floor(y * n + 0.5)
|
|
|
|
for i=0,m do
|
|
|
|
table.insert(set,x)
|
|
|
|
end
|
|
|
|
end
|
2017-11-15 14:41:34 +02:00
|
|
|
set._n = #set
|
2017-11-15 12:53:49 +02:00
|
|
|
return set
|
|
|
|
end
|
|
|
|
|
|
|
|
global.poisson_set = {}
|
2017-11-15 14:41:34 +02:00
|
|
|
function poisson_rng_next(l)
|
2017-11-15 12:53:49 +02:00
|
|
|
if not global.poisson_set[l] then
|
|
|
|
global.poisson_set[l] = generate_poisson_set(l, 1000)
|
|
|
|
end
|
2017-11-15 14:41:34 +02:00
|
|
|
return global.poisson_set[l][math.random(global.poisson_set[l]._n)]
|
2017-11-15 12:53:49 +02:00
|
|
|
end
|