Skip to content
On this page

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

module · Ruby 3.2

SecureRandom

4 class methods

Secure random number generator interface.

This library is an interface to secure random number generators which are suitable for generating session keys in HTTP cookies, etc.

You can use this library in your application by requiring it:

Example
require 'securerandom'

It supports the following secure random number generators:

  • openssl

  • /dev/urandom

  • Win32

SecureRandom is extended by the Random::Formatter module which defines the following methods:

  • alphanumeric

  • base64

  • choose

  • gen_random

  • hex

  • rand

  • random_bytes

  • random_number

  • urlsafe_base64

  • uuid

These methods are usable as class methods of SecureRandom such as SecureRandom.hex.

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

Class methods 4