ruby on rails 随机生成8位数不重复的随机函数,保存到数据库下面是代码

def key
a=[]
while (a.size < 1000) do
key=rand(99999999)
tag=false
if key > 10000000
a.each do |aa|
if aa==key
tag=true
end
end
else
tag=true
end
if tag==false
a << key
end
end
Number.destroy_all
a.each do|aa|
Number.new({:number=>aa,:bs=>0}).save!
end
render :text=>a.join("<br />")
end