Skip to content

Instantly share code, notes, and snippets.

@rageandqq
Created February 13, 2017 19:12
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 rageandqq/7ab4cf122776a97d01ba701f5da2ec85 to your computer and use it in GitHub Desktop.
Save rageandqq/7ab4cf122776a97d01ba701f5da2ec85 to your computer and use it in GitHub Desktop.
Generate an alphanumeric token of specified length
def get_token(length)
token = ''
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'.split('')
rndGen = Random.new
(1..length).each do
token << chars[(rndGen.rand * chars.length).floor] # memory efficient concatenation
end
token
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment