Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@citizen428
Created June 29, 2009 16:15
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 citizen428/137675 to your computer and use it in GitHub Desktop.
Save citizen428/137675 to your computer and use it in GitHub Desktop.
def get_random_char
(r = rand(36)) < 26 ? (?a+r).chr : (?0+r-26).chr
end
# building a string using the above method
def generate_string(len)
raise ArgumentError if len < 1
(1..len).map { get_random_char }.join
end
generate_string(7) # => "tf3c38b"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment