Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Random::Formatter#alphanumeric

alphanumeric(n=nil)

SecureRandom.alphanumeric generates a random alphanumeric string.

The argument n specifies the length, in characters, of the alphanumeric string to be generated.

If n is not specified or is nil, 16 is assumed. It may be larger in the future.

The result may contain A-Z, a-z and 0-9.

Example
require 'securerandom'

SecureRandom.alphanumeric     #=> "2BuBuLf3WfSKyQbR"
SecureRandom.alphanumeric(10) #=> "i6K93NdqiH"

If a secure random number generator is not available, NotImplementedError is raised.

See also #base64 #choose also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1