随机输出一个0到9组成的不重复的7位数
lua吧
全部回复
仅看楼主
level 5
我的想法是表,然后选一个删除一个。
2016年08月09日 00点08分 1
level 5
array={0,1,....9}接下去 求助啊 或者有其他的方法吗?
2016年08月09日 00点08分 2
level 5
自己终结
math.randomseed(os.time())
a=math.random(1,9)
b=math.random(0,9)
c=math.random(0,9)
d=math.random(0,9)
e=math.random(0,9)
f=math.random(0,9)
g=math.random(0,9)
while a==b do
b=math.random(0,9)
end
while a==c or b==c do
c=math.random(0,9)
end
while a==d or b==d or c==d do
d=math.random(0,9)
end
while a==e or b==e or c==e or d==e do
e=math.random(0,9)
end
while a==f or b==f or c==f or d==f or e==f do
f=math.random(0,9)
end
while a==g or b==g or c==g or d==g or e==g or f==g do
g=math.random(0,9)
end
print(a..b..c..d..e..f..g)
2016年08月09日 01点08分 3
level 5
这个方法太笨
2016年08月09日 01点08分 4
level 2
local random_num
random_num = function(num, result, idx)
math.randomseed(tostring(os.time()):reverse():sub(1, 6))
local n = math.random(#num)
if num[n] then
table.insert(result, num[n])
table.remove(num, n)
end
idx = idx and idx + 1 or 1
if idx < 7 then return random_num(num, result, idx) end
end
main = function()
local num = {0,1,2,3,4,5,6,7,8,9}
local result = {}
random_num(num, result)
for i, v in ipairs(result) do
print(i.." : "..v)
end
end
2016年08月10日 07点08分 6
level 11
[滑稽]实际上出现这个的几率就很低了。把数据加入到表里判断一下就可以了嘛,如果有一样的就把他给XX了[滑稽]
2016年08月10日 14点08分 7
1