lua随机数

function getrandom(nMax)
	math.randomseed(tostring(os.time()):reverse():sub(1, 6))
	local tab = {}
	local tabFin = {}
	local Rand
	for i=1,nMax do
		table.insert(tab,i)
	end
	for i=1,table.getn(tab) do
		Rand = math.random(table.getn(tab))
		while tab[Rand] == nil do
			Rand = math.random(table.getn(tab))
		end
		table.insert(tabFin,tab[Rand])
		table.remove(tab,Rand)
	end
	return tabFin
end

local testrand = getrandom(10)
for k,v in pairs(testrand) do
	print(k,v)
end

 

function getrandom(nMin,nMax)
	math.randomseed(tostring(os.time()):reverse():sub(1, 6))
	local tab = {}
	local tabFin = {}
	local Rand
	for i=1,nMax do
		table.insert(tab,i)
	end
	for i=1,table.getn(tab) do
		Rand = math.random(table.getn(tab))
		while tab[Rand] == nil do
			Rand = math.random(table.getn(tab))
		end
		table.insert(tabFin,tab[Rand])
	end
	Rand = math.random(table.getn(tab))
	while tab[Rand] == nil do
		Rand = math.random(table.getn(tab))
	end
	table.remove(tab,Rand)
	return (tabFin[Rand]+nMin-1)
end
print(getrandom(1,10))

  

原文地址:https://www.cnblogs.com/byfei/p/2932074.html