12345678910111213141516171819 |
- RandomUtil = {}
- -- 示例:"1#80|2#10|3#10"
- -- 80%的概率选中1
- -- 10%的概率选中2
- -- 80%的概率选中3
- function RandomUtil.selectKey(configStr)
- local randomValue = math.random(0, 100)
- local configMap = string.toStringStringMap(configStr, "#", "|")
- local count = 0
- for key, value in pairs(configMap) do
- count = count + tonumber(value)
- if randomValue <= count then
- return tonumber(key)
- end
- end
- return 0;
- end
|