Create a Random String in Ruby
25 Feb 2012Not the hardest of tasks, but nevertheless a discussed one: Creating random strings in ruby. And that would be base62 encoded strings, so that they can be used in URLs.
So what to do? Using this solution? Not random enough.
What about the solutions described in this blog post? Promising. And I like the ActiveSupport::SecureRandom method best.
But since ruby1.9 we have SecureRandom natively and want to use that. Also simply removing the characters that do not fit in base62 will produce strings of differing length and we don’t want that. What to do?
Mashup!
SecureRandom.base64(6).gsub(/[$=+\/]/,65.+(rand(25)).chr)
ProTip: play around with the base64 parameter to change the length of the generated string.