首页 > 代码库 > lua 1*100(随机乘与递加)

lua 1*100(随机乘与递加)

n = 0
function ()
local n = n + 1
while n < 100 do
math.random(100)
end
end

随机乘以1到100以内的数↑

 

全排列,可以产生N!种排列:

local N = 100
local array = {}
for i = 1 , N do
array[i] = i 
end
for i = 1 , N do
local j = math.random(N - i + 1) + i - 1;
array[i],array[j] = array[j],array[i]
end
for i = 1 , N do
print(array[i])
end

从1加到100:

i = 1;
sum = 0;
while (i<=100) do
 sum = sum + i
 i = i + 1
end
print (sum)

简算式:

sum = 100 * (100-1)/2

  

lua 1*100(随机乘与递加)