RandomUtil.lua 478 B

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