Skip to content

Instantly share code, notes, and snippets.

@sergii
Forked from umutakturk/random_string.rb
Created January 7, 2023 23:03
Show Gist options
  • Save sergii/5542352f353e189b6a47a606e76e7a7e to your computer and use it in GitHub Desktop.
Save sergii/5542352f353e189b6a47a606e76e7a7e 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