我目前正在为“A”生成一个8字符伪随机大写字符串。。“Z”:
value = ""; 8.times{value << (65 + rand(25)).chr}
但它看起来不干净,而且不能作为参数传递,因为它不是一个单独的语句。要获取混合大小写字符串“a”。。“z”加“A”。。“Z”,我将其更改为:
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
但它看起来像垃圾。
有人有更好的方法吗?
我目前正在为“A”生成一个8字符伪随机大写字符串。。“Z”:
value = ""; 8.times{value << (65 + rand(25)).chr}
但它看起来不干净,而且不能作为参数传递,因为它不是一个单独的语句。要获取混合大小写字符串“a”。。“z”加“A”。。“Z”,我将其更改为:
value = ""; 8.times{value << ((rand(2)==1?65:97) + rand(25)).chr}
但它看起来像垃圾。
有人有更好的方法吗?
当前回答
鉴于:
chars = [*('a'..'z'),*('0'..'9')].flatten
可以作为参数传递的单个表达式允许重复字符:
Array.new(len) { chars.sample }.join
其他回答
从Ruby 2.5开始,使用SecureRandom.andlettery非常简单:
len = 8
SecureRandom.alphanumeric(len)
=> "larHSsgL"
它生成包含A-Z、A-Z和0-9的随机字符串,因此应适用于大多数用例。它们是随机安全生成的,这可能也是一个好处。
这是一个基准,用于将其与支持率最高的解决方案进行比较:
require 'benchmark'
require 'securerandom'
len = 10
n = 100_000
Benchmark.bm(12) do |x|
x.report('SecureRandom') { n.times { SecureRandom.alphanumeric(len) } }
x.report('rand') do
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
n.times { (0...len).map { o[rand(o.length)] }.join }
end
end
user system total real
SecureRandom 0.429442 0.002746 0.432188 ( 0.432705)
rand 0.306650 0.000716 0.307366 ( 0.307745)
因此,rand解决方案只需要SecureRandom时间的3/4。如果您生成大量字符串,这可能很重要,但如果您只是时不时地创建一些随机字符串,我会始终使用更安全的实现,因为它也更容易调用,更显式。
这里有一个灵活的解决方案,允许重复数据:
class String
# generate a random string of length n using current string as the source of characters
def random(n)
return "" if n <= 0
(chars * (n / length + 1)).shuffle[0..n-1].join
end
end
例子:
"ATCG".random(8) => "CGTGAAGA"
还可以允许某个字符更频繁地出现:
"AAAAATCG".random(10) => "CTGAAAAAGC"
说明:上面的方法接受给定字符串的字符并生成足够大的数组。然后,它将其洗牌,取出前n个项目,然后将其合并。
我喜欢使用的另一种方法:
rand(2**256).to_s(36)[0..7]
如果您真的对正确的字符串长度有疑问,请添加ljust:
rand(2**256).to_s(36).ljust(8,'a')[0..7]
这是基于一些其他答案,但它增加了一点复杂性:
def random_password
specials = ((32..47).to_a + (58..64).to_a + (91..96).to_a + (123..126).to_a).pack('U*').chars.to_a
numbers = (0..9).to_a
alpha = ('a'..'z').to_a + ('A'..'Z').to_a
%w{i I l L 1 O o 0}.each{ |ambiguous_character|
alpha.delete ambiguous_character
}
characters = (alpha + specials + numbers)
password = Random.new.rand(8..18).times.map{characters.sample}
password << specials.sample unless password.join =~ Regexp.new(Regexp.escape(specials.join))
password << numbers.sample unless password.join =~ Regexp.new(Regexp.escape(numbers.join))
password.shuffle.join
end
本质上,它确保密码长度为8-20个字符,并且至少包含一个数字和一个特殊字符。
Array.new(8).inject(""){|r|r<<('0'..'z').to_a.shuffle[0]} # 57
(1..8).inject(""){|r|r<<('0'..'z').to_a.shuffle[0]} # 51
e="";8.times{e<<('0'..'z').to_a.shuffle[0]};e # 45
(1..8).map{('0'..'z').to_a.shuffle[0]}.join # 43
(1..8).map{rand(49..122).chr}.join # 34