Generate a random, friendly confirmation code in pure Ruby
Here’s a really handy trick for generating a pseudo-random alphanumeric code in pure Ruby – no gems or dependencies needed.
>> rand(36**6).to_s(36) => "a7yr8r"
Note that 36**6 = 2,176,782,336 = or about 2 Billion combinations. Depending on your needs that may or may not be sufficiently large to avoid collisions.
Should you need it, generating longer combinations is trivial by simply changing the exponent.
>> rand(36**24).to_s(36) => "yrt4ols2ke1jm44w9bxvvp9l"
So, what does the .to_s(36) do? I’ll give you a hint – 36 is called the radix, and you can read more about it here.