Skip to content

Instantly share code, notes, and snippets.

@nawada
Created September 12, 2014 05:00
Show Gist options
  • Save nawada/82d04b5e9d0fec0c8fb9 to your computer and use it in GitHub Desktop.
Save nawada/82d04b5e9d0fec0c8fb9 to your computer and use it in GitHub Desktop.
ASCIIコード範囲内でのランダム文字列出力
# encoding: utf-8
totalLength = 10 # 出力桁数
symbolLen = 0 # 記号の出力桁数
numericLen = 3 # 数字の出力桁数
symbols = (("!".."/").to_a + (":".."@").to_a + ("[".."`").to_a + ("{".."~").to_a)
numerics = (0..9).to_a
upperAlpha = ("A".."Z").to_a
lowerAlpha = ("a".."z").to_a
# アルファベットで使える文字数
alphaLen = totalLength - (symbolLen + numericLen)
symbols = symbols.sample(symbolLen)
numerics = numerics.sample(numericLen)
alpha = (upperAlpha + lowerAlpha).sample(alphaLen)
puts (symbols + numerics + alpha).shuffle.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment