Created
July 17, 2013 01:48
-
-
Save christian-marie/6017019 to your computer and use it in GitHub Desktop.
Simple random string generating lambda in Ruby with a small chance that the length of your string will be one less than you expect.
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
random_string = -> length, radix=36 { rand(radix ** length).to_s(radix) } | |
# Random string of length 42, maybe 41. | |
random_string.call(42) | |
# Four random digits | |
random_string.call(4, 10) | |
# Four random hex digits | |
random_string.call(4, 16) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment