Skip to content

Instantly share code, notes, and snippets.

@harlantwood
Created November 9, 2009 10:24
Show Gist options
  • Save harlantwood/229859 to your computer and use it in GitHub Desktop.
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
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