Skip to content

Instantly share code, notes, and snippets.

@umutakturk
Last active January 7, 2023 23:03
Show Gist options
  • Save umutakturk/3835467 to your computer and use it in GitHub Desktop.
Save umutakturk/3835467 to your computer and use it in GitHub Desktop.
Generate random string in Ruby.
def random_string(length = 6)
rand(36**length).to_s(36)
end
def random_string
rand(2**1024).to_s(36).upcase[0..9]
end
def random_string(length)
chars = ('A'..'Z').to_a + ('a'..'z').to_a + ('0'..'9').to_a
(1..length).map {
chars[rand(chars.length)]
}.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment