LoginSignup
45
37

More than 5 years have passed since last update.

数値、アルファベットからランダムなn文字の文字列を生成する

Posted at

0〜9、a〜z、A〜Zの中から5文字ランダムに抽出し、文字列に変換する

((0..9).to_a + ("a".."z").to_a + ("A".."Z").to_a).sample(5).join
 # => "Aw5EV" 

その他、文字列配列生成サンプル

(0..9).to_a
 # => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

("a".."z").to_a
 # => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] 

("A".."Z").to_a
 # => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] 

("A".."z").to_a
 # => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"] 

(("a".."z").to_a + ("A".."Z").to_a)
 # => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] 
45
37
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
45
37