def rand_password(length = 6,type = 0)
#密码过短提示
return "length is too short!" if length < 6
#获取必要的方法参数
if type = 0
length < 12 ? @pastype = 1 : @pastype = 2
else
@pastype = type
end
case @pastype
when 1
#计算算法,适合短密码
chrArr = []
chrArr << (48+rand(10)).chr
chrArr << (65+rand(26)).chr
chrArr << (97+rand(26)).chr
chrArr << Specialnum[rand(Specialnum.size)].chr
chrArr.delete_at(rand(4))
(length-3).times{chrArr << (33+rand(93)).chr}
ret = ""
for i in 0...length
ret += chrArr.delete_at(rand(length-i))
end
return ret
when 2
#校验算法,适合长密码
loop do
str_type = 0
ret = ''
ret = Array.new(length).inject(''){|str,x| str+(33+rand(93)).chr}
str_type += 1 if re.index(/[A-Z]/)
str_type += 1 if re.index(/[a-z]/)
str_type += 1 if re.index(/\d/)
str_type += 1 if re.index(/[^\w]/)
return ret if str_type > 2
end
else
#意外出错返回
return "Type Error!"
end
end[/pre]