Created
January 9, 2010 15:12
-
-
Save yosshi/272939 to your computer and use it in GitHub Desktop.
ランダムな文字列を生成する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ランダムな文字列を生成する。 | |
# 引数 _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