Skip to content

Instantly share code, notes, and snippets.

@christian-marie
Created July 17, 2013 01:48
Show Gist options
  • Save christian-marie/6017019 to your computer and use it in GitHub Desktop.
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.
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