lua 学习笔记10

lua 学习笔记10

/********************/
 调用数学库第一个例子
/********************/
 — 产生10个随机的1-10之间不重复的数
 randTen = {}
 
 for i=1,10 do
 randTen[i] = i
 end
 
 function getARand()
    for nowNumber=10,1,-1 do
  local curIdx = math.random(1,nowNumber)
  print(randTen[curIdx])
  for next = curIdx,nowNumber – 1 do
      randTen[next] = randTen[next + 1]
        end
    end
 end
 
 getARand()
 
 
 –[[
 output:
2
9
1
10
7
5
8
4
3
6
 ps:
  不知道为什么,加上math.randomseed(os.time())后,反而出现随机数单一的情况
 –]]
稍微修改下,成为任意数的版本
 function getARand(randTen,max)
    if(max == 0) then return end
   
 local curIdx = math.random(1,max)
 print(randTen[curIdx])
 for next = curIdx,max – 1 do
  randTen[next] = randTen[next + 1]
    end
   
    getARand(randTen,max – 1)
 end
 
 function randDifNumber(max)
     local randTen = {}
     for i=1,max do
                randTen[i] = i
     end
 
     getARand(randTen ,max)
 end
 
 randDifNumber(100)

One thought on “lua 学习笔记10

  1. wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold
    wow gold -229110063361902

Leave a Reply

Your email address will not be published. Required fields are marked *