pool
x for y timesuser_9968637368
lua
3 years ago
1.0 kB
17
Indexable
function randomWeight(numbers, desire, percent)
local pool = {}
for i=1, numbers do
pool[i] = {i, 100/numbers}
end
pool[desire] = {desire, percent}
local poolsize = 0
for k,v in pairs(pool) do
poolsize = poolsize + v[2]
end
local selection = math.random(1,poolsize)
for k,v in pairs(pool) do
selection = selection - v[2]
if (selection <= 0) then
return v[1]
end
end
end
function howManyIn(tbl, val)
local much = 0
for i, v in pairs(tbl) do
if v == val then
much = much + 1
end
end
return much
end
weight = io.read()
print("color 2 is chosen to be "..weight.." of 100 weighted")
local trial = {}
for o=1, 25 do
table.insert(trial, randomWeight(5, 2, weight))
end
print("color 1 repeated "..howManyIn(trial, 1).."x")
print("color 2 repeated "..howManyIn(trial, 2).."x")
print("color 3 repeated "..howManyIn(trial, 3).."x")
print("color 4 repeated "..howManyIn(trial, 4).."x")
print("color 5 repeated "..howManyIn(trial, 5).."x")
Editor is loading...