DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
String.random
class String
def self.random(length=20)
chars = ('a'..'z').to_a + ('A'..'Z').to_a + ("0".."9").to_a
hash = ""; length.times { hash << chars[rand(chars.size)] }; hash
end
end





Comments
Snippets Manager replied on Tue, 2008/03/11 - 10:39am
Snippets Manager replied on Fri, 2008/06/27 - 2:54pm
class String def self.random(length=20) @@chars ||= ('a'..'z').to_a + ('A'..'Z').to_a + ("0".."9").to_a (1..length).map { @@chars[rand(@@chars.size)] }.to_s end endSnippets Manager replied on Wed, 2007/05/16 - 11:08pm
(1..length).map{i=rand(62);(i+(i<10?48:i<36?55:61)).chr}*''Cryptic, but eh.