Unbias a random generator: Difference between revisions

Content added Content deleted
(added Lua example)
(→‎{{header|Lua}}: redefined randN to not check that bias is within range or store chance)
Line 185: Line 185:
<lang lua>
<lang lua>
local function randN(n)
local function randN(n)
if n < 3 or n > 6 then error "Invalid bias" end
local chance = 1/n
return function()
return function()
if math.random() < chance then return 1 else return 0 end
if math.random() < 1/n then return 1 else return 0 end
end
end
end
end