math.randomseed(os.time())
local function hit(a,b,m)
local c=math.floor((b+a-1)/a)
if m.num>=c then
m.num=0
m.hit=false
end
local result=false
m.num=m.num+1
if not m.hit then
local y=a+math.floor((m.num*m.num*m.num)/(c*c*c)*(b-a))
local x=math.random(1,b)
if x<=y then
m.hit=true
result=true
end
end
return result
end
local m={num=0,hit=false}
for i=1,20 do
io.write(string.format("i(%s),result(%s),num(%s)\n",i,hit(10,100,m),m.num))
end
一种体验很好的,在给定概率内,必中一次的算法