Given a set a yet unknown values in an array/table, where each value contains a number, how would I make it so that the values with higher numbers have a larger chance of being picked?
I did this in Lua and I came up with an extremely inefficient method that didn't even work some reason:
Example:
"module.InsertTokens(5,'a')
--The function above would insert the value "a" five times into an array
module.RemoveId('a') --this is supposed to remove all values of a
print(#module.Tokens)"
2 --should be 0 --why does it return 2?? D:
function module.InsertTokens(amount, id)
for i = 1, amount do --amount times
print'inserting'
module.Tokens[#module.Tokens+1]=id
end
end
function module.RemoveId(id)
for index, v in pairs(module.Tokens) do
if v == id then
print'removing'
table.remove(module.Tokens, index)
end
end
end
Is there some sort of algorithm or better logic I can use? (Also if you can, point out what I did wrong with the code above?)
Aucun commentaire:
Enregistrer un commentaire