How to Generate a Random Password Using Ruby? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Generating a random password is a fundamental task in cybersecurity and application development. Creating a secure random password is very easy with the Ruby library. This article will show how to generate a random password in Ruby in Python. Generate a Random Password Using RubyBelow are the code examples of how to Generate a Random Password using Ruby in Python: Example 1: Using SecureRandom MethodIn Ruby, the SecureRandom module is essential for generating cryptographically secure random numbers and strings. It guarantees high levels of unpredictability and security, making it ideal for sensitive tasks such as password generation and cryptographic key creation. Example: In this example, the code below uses the SecureRandom library to produce random passwords. The function 'generate_password' generates alphanumeric passwords with a specified length, set by default to 8 characters. An illustration showcases generating and displaying a 12-character password. Ruby require 'securerandom' def generate_password(length = 8) SecureRandom.alphanumeric(length) end puts generate_password(12) OutputeA3OnHSRBc2V Example 2: Using Predefined Set of CharactersIn this example, the function 'generate_password' crafts a random password with the desired length, blending letters (both upper and lower case), digits, and special characters. The demonstration exhibits generating and showcasing a 12-character password. Ruby def generate_password(length = 8) characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$' password = (0...length).map { characters[rand(characters.length)] }.join end puts generate_password(12) OutputYGY14eZuCuUx Comment More infoAdvertise with us Next Article Ruby | Random uuid() function V vishal_shevale Follow Improve Article Tags : Ruby Python-Library Similar Reads How to generate a random String in Ruby? In this article, we will learn how to generate a random String in Ruby. Approach to generate a random String in Ruby:When you need to generate a random alphanumeric string of a specified length in Ruby, you have a couple of options. If you are using Ruby version >= 2.5, Ruby, you can simply go wi 2 min read How to Get User Input in Ruby? The input of users is a very vital part of many programs which allows customization and interaction. In Ruby, there are various methods to get input from the user, each having its own benefits and use cases. This article will discuss two different ways to obtain user input in Ruby. Table of Content 2 min read How to Build an API With Ruby on Rails? Ruby on Rails API refers to the application programming interface (API) framework provided by the Ruby on Rails (Rails) web application framework. It allows developers to build and expose APIs for their web applications efficiently. Ruby on Rails is a popular web development framework written in the 4 min read Ruby | Random rand() function Random#rand() : rand() is a Random class method which generates a random value. Syntax: Random.rand() Parameter: Random values Return: generates a random value. Example #1 : Ruby # Ruby code for Random.rand() method # declaring Random value date_a = Random.rand() # new arbitrary random value puts 1 min read Ruby | Random uuid() function Random#uuid() : uuid() is a Random class method which checks returns a random v4 UUID (Universally Unique IDentifier). Syntax: Random.uuid() Parameter: Random values Return: a random v4 UUID (Universally Unique IDentifier). Example #1 : Ruby # Ruby code for Random.uuid() method # loading library req 1 min read Ruby | Random new_seed() function Random#new_seed() : new_seed() is a Random class method which returns a random seed value. Syntax: Random.new_seed() Parameter: Random values Return: a random seed value Example #1 : Ruby # Ruby code for Random.new_seed() method # declaring Random value date_a = Random.new_seed() # new arbitrary ran 1 min read Like