How to Generate Random Number in Ruby

Zeeshan Afridi Feb 02, 2024
  1. Create Random Numbers in Ruby
  2. Use rand() to Generate Random Numbers in Ruby
  3. Use Kernel#srand to Seed the Random Number Generator
  4. Create a Random String in Ruby
  5. Conclusion
How to Generate Random Number in Ruby

This article will explain how to create random numbers in Ruby.

Create Random Numbers in Ruby

There are a few different ways to generate random numbers in Ruby.

  1. Using the rand() method.
  2. The Kernel#srand method is used to seed the random number generator.

The rand() method generates a random float between 0 and 1. This can be useful for generating random numbers for simulations or games.

The rand() method is used in conjunction with the floor method to generate a random integer. This will return a random integer between 0 and the number passed to the floor method.

For example, the code snippet below generates a random integer between 0 and 10.

rand(10).floor

Finally, the srand() method can be used to seed the random number generator. This is useful if you want to develop the exact sequence of random numbers on multiple runs of the program.

Use rand() to Generate Random Numbers in Ruby

The rand() method is a popular way to generate random numbers in programming. It is a simple method that uses an algorithm to generate a pseudorandom number between 0 and 1.

This number is then multiplied by the desired range and rounded to the nearest integer to get the final result.

The rand() method is often used in games and simulations to create a realistic and unpredictable environment. It is also used in statistical sampling and data analysis.

If you want to make a random number between 0 and 10, you can use the rand method like this.

Code Example:

rand(10)
puts rand(10)

Output:

5

The following example generates a random number between 1 and 10 using the rand() method.

Code Example:

rand(1..10)
puts rand(1..10)

Output:

6

Use Kernel#srand to Seed the Random Number Generator

You can also use the Kernel#srand method to seed the random number generator, giving you more control over the random numbers generated. When called with a seed, the Kernel#srand method seeds the random number generator, making it possible to generate repeatable sequences of random numbers.

If no seed is provided, the generator is seeded with a combination of time, the process ID, and a sequence number. Calling Kernel#srand with the identical seed will always make the same sequence of random numbers.

This can be useful for debugging or for creating deterministic simulations. However, using the same seed in production is generally bad, as it can make your application more predictable and easier to exploit.

If you need to generate repeatable sequences of random numbers, it’s generally better to use a library that provides a dedicated API, such as the Mersenne Twister.

Code Example:

puts srand 1234
puts [rand, rand]

puts [rand(10), and(1000)]

puts srand 1234
puts [rand, rand]

Output:

68269704279078804018695389408585286811
0.1915194503788923
0.6221087710398319
4
1000
1234
0.1915194503788923
0.6221087710398319

Create a Random String in Ruby

There are a few ways to generate random strings in Ruby. One way is to use the Array.sample method.

This method will select a random element from an array.

Code Example:

arr = %w[a b c d e]
arr.sample
puts arr.sample

Output:

c

Another way to generate random strings is to use the SecureRandom.hex method. This will generate a random string of hexadecimal characters.

Code Example:

require 'securerandom'
inp = SecureRandom.hex(10)
puts inp

Output:

e30c8046b7fac0627436

You can also use the SecureRandom#base64 method to generate a random string of base64 characters.

Code Example:

require 'securerandom'
inp = SecureRandom.base64(10)
puts inp

Output:

kc8/UBu0aFA8eg==

If you require a random string of a specific length, you can use the SecureRandom#random_bytes method. This will generate a random string of bytes:

require 'securerandom'
inp = SecureRandom.random_bytes(10)
puts inp

Output:

�gՙP0�n[

Finally, if you need a random alphanumeric string, you can use the SecureRandom#urlsafe_base64 method.

Code Example:

require 'securerandom'
inp = SecureRandom.urlsafe_base64(10)
puts inp

Output:

ce6gAf9EyVdPMA

Conclusion

The article concludes that the rand can be useful for generating random numbers in your Ruby programs. The method takes an optional argument, the upper limit of the range of digits that can be generated.

Another method is the Kernel#srand to seed the random number generator. You can also create a random string with the ruby language.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn