在场景种是否有遇到过概率比来的到值呢
这里提供一个Lua的代码做思路。
function test(table1, table2)
if table1== nil or table2== nil then
return
end
local price = 0
local pricetable ={}
for _,v in ipairs(table2) do
price = v + price
table.insert(pricetable,price)
end
local m_random = math.random(1,price)
for i,v in ipairs(pricetable) do
if m_random <= v then
return table1[i]
end
end
end
本文介绍了一种使用Lua实现的概率选择算法,通过遍历两个表格并利用随机数生成器,实现了根据权重进行元素选择的功能。该算法适用于需要按概率分布进行随机选择的场景。
529

被折叠的 条评论
为什么被折叠?



