Created
February 13, 2017 19:12
-
-
Save rageandqq/7ab4cf122776a97d01ba701f5da2ec85 to your computer and use it in GitHub Desktop.
Generate an alphanumeric token of specified length
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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