RosettaCodeData/Task/Knuths-algorithm-S/Pluto/knuths-algorithm-s.pluto
2026-04-30 12:34:36 -04:00

29 lines
660 B
Text

require "table2"
local function s_of_n_creator(n)
local s = table.rep(n, 0)
local next = 1
local m = n
return function(item)
if next <= n then
s[next] = item
next += 1
else
m += 1
if math.random(m) <= n then
local t = math.random(n)
s[t] = item
if next <= t then next = t + 1 end
end
end
return s
end
end
local freq = table.rep(10, 0)
for r = 0, 99_999 do
local son = s_of_n_creator(3)
for d = 48, 56 do son(d) end
for son(57) as d do freq[d - 47] += 1 end
end
print(freq:concat(", "))