已知整数池子里有n个整数分别为A1,A2,A3.....,每个整数对应的占比已知为R1,R2,R3.......占比之和也已知为m,求随机取出某个整数的算法。有没有比下面更节省效率的方法。
function GetRandomInteger takes integer zsc returns integer
local integer a = 1
local integer i = 0
local real sjs
set sjs = GetRandomReal(0, m)
loop
set sjs = sjs - Ra //Ra表示第a个整数的占比
exitwhen sjs<=0
set a = a + 1
endloop
set i = Aa //Aa表示第a个整数
return i
endfunction
function GetRandomInteger takes integer zsc returns integer
local integer a = 1
local integer i = 0
local real sjs
set sjs = GetRandomReal(0, m)
loop
set sjs = sjs - Ra //Ra表示第a个整数的占比
exitwhen sjs<=0
set a = a + 1
endloop
set i = Aa //Aa表示第a个整数
return i
endfunction