level 1
不要钱的薄荷糖
楼主
--参数是由每个几率组成的表,返还值是随机序号N(1~size之间)
function randGetNum(chance)
local size = table.getn(chance); --比如chance={2000,2000,2000,2000,2000}
local sum = 0;
for i=1,size do
sum = sum + chance[i]; --比如for循环结束时sum=10000
end
local temp = {};
for i=1,size do
if i==1 then
temp[i] = chance[i];
else
temp[i] = temp[i-1] + chance[i]; --比如for循环结束时temp={2000,4000,6000,8000,10000}
end
end
local index = RandGetBetween(1,sum); --(1,10000)
for i=1,size do
if index <= temp[i] then --比如index=5000则 index<=6000,则return 序号3
return i;
end
end
end
关于这个参数,for循环为什么要计算一个sum,然后temp表也没看明白什么意思。
2015年10月23日 09点10分
1
function randGetNum(chance)
local size = table.getn(chance); --比如chance={2000,2000,2000,2000,2000}
local sum = 0;
for i=1,size do
sum = sum + chance[i]; --比如for循环结束时sum=10000
end
local temp = {};
for i=1,size do
if i==1 then
temp[i] = chance[i];
else
temp[i] = temp[i-1] + chance[i]; --比如for循环结束时temp={2000,4000,6000,8000,10000}
end
end
local index = RandGetBetween(1,sum); --(1,10000)
for i=1,size do
if index <= temp[i] then --比如index=5000则 index<=6000,则return 序号3
return i;
end
end
end
关于这个参数,for循环为什么要计算一个sum,然后temp表也没看明白什么意思。