lua 实现概率事件,比如0.1的概率

  • Post author:
  • Post category:其他


原理:随机数随机1,10000个数,其中随机到到数和10000*0.1(概率)=1000进行比较,随机数大于1000则不选中,反之则选中。


local successCount =0;

local commitPercent = 0.1;

local percent = commitPercent*10000;

for i=1,100 do

local randomNum = math.random(1,10000);

sysLog(“–randomNum–“..randomNum..”–commitPercent–“..percent)

if randomNum>percent then

sysLog(“–false–“)

else

sysLog(“–true–“)

successCount = successCount +1;

end

end

sysLog(“–successCount–“..successCount)



版权声明:本文为pzf348155457原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。