lua 不重复随机数算法 比如1-7 取7次 每次不能重复 高手请帮帮忙 急!!! 30
1个回答
展开全部
function RandomIndex(tabNum,indexNum)
indexNum = indexNum or tabNum
local t = {}
local rt = {}
for i = 1,indexNum do
local ri = math.random(1,tabNum + 1 - i)
local v = ri
for j = 1,tabNum do
if not t[j] then
ri = ri - 1
if ri == 0 then
table.insert(rt,j)
t[j] = true
end
end
end
end
return rt
end
--以下为用例
tab = {1,2,3,4,5,6,7}
s = RandomIndex(7,7)
for i = 1,7 do
print(tab[s[i]])
end
原帖地址
http://user.qzone.qq.com/415431019/blog/1308626965
indexNum = indexNum or tabNum
local t = {}
local rt = {}
for i = 1,indexNum do
local ri = math.random(1,tabNum + 1 - i)
local v = ri
for j = 1,tabNum do
if not t[j] then
ri = ri - 1
if ri == 0 then
table.insert(rt,j)
t[j] = true
end
end
end
end
return rt
end
--以下为用例
tab = {1,2,3,4,5,6,7}
s = RandomIndex(7,7)
for i = 1,7 do
print(tab[s[i]])
end
原帖地址
http://user.qzone.qq.com/415431019/blog/1308626965
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询