Ruby Generate Random String

Random strings are helpful features in various cases such as encryption, data seeds, and more. Although it’s impossible to create a genuinely arbitrary value using deterministic machines, we can get a set of pseudorandom values that appear randomly generated.

This guide will show you how to generate a random string using Ruby. Since a string is an array of alphanumeric characters, we will start with the basics and generate random numbers and characters.

Generating a Random Number

Before we focus on generating random strings in Ruby, let us learn how to generate a random number in Ruby.

We use the rand method to generate random numbers. If you call the rand method with no arguments, it generates a floating number in the range of 0.0 and 1.0

=> 0.041586206067679155

The rand function also allows you to pass an integer as an argument. If the method has a provided integer value, it will generate a random integer value in the range of 0 to the specified value.

=> 5

In the example above, every time you run the method above, you will get a value between 0 and 10, exclusive.

Suppose you want to generate a random number within a specific range. In such a scenario, you can specify the range as:


For example, to generate a random value between 30 and 60:

=> 42

The example above is exclusive of the end value. This means 60 will not be included as a random value.

If you want to include an end value, use the double-dot notation as:

=> 60

Generating a Random String

Although there are various ways to generate a random string in Ruby, we will stick with a simple one that utilizes built-in Ruby methods and functionality.

Step 1 – Generate Array of Characters

The first method is to generate an array of characters. To do this, we will use the range method in ruby.

For example, the following code generates an array of lower case characters.


Next, we need to convert the output from the above code into an array. We can use the to_a method as:


The above method will give us an array of characters.

You can only use lower case characters only. You can generate uppercase characters as:


Step 2 – Shuffle the generated characters

In the previous example, we generate an array of characters in sequential order. Hence, the values are not in random order.

To introduce randomness for every time the block is called, we can use the shuffle method.

The shuffle method will return a new array with the elements in the previous array shuffled.

Take the first example array and shuffle it.


In this example, the characters are shuffled in a random order every time we run the code.

Step 3 – Convert to String

Up to this point, we have only created an array of characters. The next step is to convert the characters in the array to a single string, which we can achieve using the join method.

For example:

=> "mixwnezhjckusqogyprvfdblta"

Running the code above will return a random string of lowercase characters.

Step 4 – More Randomness

We can introduce more randomness to our generated string by introducing uppercase characters and numerical values.

We can do this by:

[*'a'..'z', *0..9, *'A'..'Z']

The example above will generate an array of lowercase, uppercase, and numeric values.

To randomize them, use the shuffle method and join to create a string. Below is an example output:

[*'a'..'z', *0..9, *'A'..'Z'].shuffle.join

Suppose we want to get a string of a specific length. For example, to generate a string of length 10. We can pass the range to the shuffle method as:

[*'a'..'z', *0..9, *'A'..'Z'].shuffle[0..10].join
=> "ncNerKw78MS"

Method 2 -SecureRandom Module

Ruby also provides the securerandom package for generating random strings. It supports hex, base64, and bytes.

To use it, import it as:

require 'securerandom'
=> true

Generate a random base64 value as:

=> "cXzPK1zBBBc/Odu1FSapwg=="

For hex, enter:

=> "86dc7bcec3962153efef36585696920f"

Finally, using bytes:

=> "\xE6)\x8E\xF4\xD9\xE1w\x1DU\xA2\x7F\x86\x97z\b."


This guide covers how to generate random values in Ruby using prebuilt methods and modules.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list