Skip to content

Instantly share code, notes, and snippets.

@yosshi
Created January 9, 2010 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yosshi/272939 to your computer and use it in GitHub Desktop.
Save yosshi/272939 to your computer and use it in GitHub Desktop.
ランダムな文字列を生成する
# ランダムな文字列を生成する。
# 引数 _length_ を指定すると生成桁数を指定することができます(デフォルト 8 桁)。
def getRandomString (length = 8)
if length == 0
length = 8
end
source=("a".."z").to_a + ("A".."Z").to_a + (0..9).to_a + ["_","-","."]
key=""
length.times{ key+= source[rand(source.size)].to_s }
return key
end
# 例: 10 桁の文字列を取得
puts getRandomString(ARGV[0].to_i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment