Created
December 14, 2018 17:27
-
-
Save samueleaton/8e520d7e052ad0da794e9cb7fbf9bc4f to your computer and use it in GitHub Desktop.
Because sometime your need a big alphanumeric string in Crystal
This file contains 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 rand_str(length = 16, supplemental_chars = "") | |
res = [] of Char | |
chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" + supplemental_chars | |
length.times do { |i| res << chars[rand(chars.size)] } | |
res.join() | |
end | |
rand_str 32 | |
#=> xkURIpYnqbRVj893pr23LhhxeukFTu5f | |
# more characters can be used using the 2nd arg | |
rand_str 48, "!@#$%^&*" | |
#=> !JzRCOtU%Xr38c*Un$bS3VZygDIfo4lFK#F@^TZAtd8I6QSG |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment