LoginSignup
2

More than 5 years have passed since last update.

ランダムな文字列生成

Last updated at Posted at 2014-02-26

Qiita登録したのでテスト投稿も兼ねて

シンプルにRubyっぽく、ランダムな文字列生成

[*:a..:z,*0..9].sample(8).join

追記

Array#sample は、同じ要素は重複して拾ってこないんですね。
勘違いしてました。
(size以上の数はsampleされない)

なので同じ文字の重複を許して、ランダムな文字列を生成するなら、

Array.new(8){[*:a..:z,*0..9].sample}.join

こんな感じ?

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
2