RosettaCodeData/Task/Unbias-a-random-generator/Pluto/unbias-a-random-generator.pluto
2026-04-30 12:34:36 -04:00

22 lines
470 B
Text

local fmt = require "fmt"
local biased = |n| -> math.random() < 1 / n
local function unbiased(n)
while true do
local a = biased(n)
local b = biased(n)
if a != b then return a end
end
end
local m = 50_000
local f = "%d: %2.2f%% %2.2f%%"
for n = 3, 6 do
local c1, c2 = 0, 0
for _ = 1, m do
if biased(n) then c1 += 1 end
if unbiased(n) then c2 += 1 end
end
fmt.print(f, n, 100 * c1 / m, 100 * c2 / m)
end