zoukankan      html  css  js  c++  java
  • 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))
    

      

  • 相关阅读:
    float
    老师的通病
    无题
    BufferedReader
    剩余定理
    ActionScript 多图加载 按图顺序索引
    C++ Socket 编程
    设计高可用和高负载的网站系统
    提高网站速度的最佳实践【翻译】
    把哈希表存储到数据库中
  • 原文地址:https://www.cnblogs.com/byfei/p/2932074.html
Copyright © 2011-2022 走看看