Created
November 9, 2009 10:24
-
-
Save harlantwood/229859 to your computer and use it in GitHub Desktop.
generate random n character strings, encoded in base 64, with completely URL-safe characters
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
url_safe_64_chars = ( 0..9 ).to_a + ( 'a'..'z' ).to_a + ( 'A'..'Z' ).to_a + %w[ - _ ] | |
random_hashcodes = 100.times.map{ |i| url_safe_64_chars[ rand * ( 64 + 1 ) ] }.join.scan( /.{10}/ ) | |
# Returns (e.g.) => ["D6sccOU_tN", "jjwXK3_zxd", "395npb8lPg", "uYPpDkyIBE", "PBbiprpK7w", "KihPOuTTEu", "ztW8GAKRuG", "0Abw9T6-CG", "wlrPjnLUHf"] | |
# More info on base 64 encoding: http://en.wikipedia.org/wiki/Base64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment