Skip to content

Instantly share code, notes, and snippets.

@hannestyden
Created August 27, 2010 14:52
Show Gist options
  • Save hannestyden/553505 to your computer and use it in GitHub Desktop.
Save hannestyden/553505 to your computer and use it in GitHub Desktop.
module FixedLengthRandomString
extend self
ALPHANUMS = [ ('a'..'z'), ('A'..'Z'), ('0'..'9') ].map { |r| r.to_a }.flatten
def random(length)
(0...length).map { ALPHANUMS.sample }.join
end
end
FixedLengthRandomString.random(10) # => "HusgO65MWj"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment