{

How to get Random Number from an array in Ruby with examples


This tutorial shows, multiple ways to get a random number from an array. One way is using the sample method, another way using shuffle.first and another way using retrieve an array with index, where index value generated using rand function.

You can get single or multiple random values from a given array.

Generate Random Number with Array sample method

An array contains the sample method which returns random values. It accepts optional arguments.

It was introduced in the Ruby 1.9.1 version, you can use the array. choice method below 1.9.1 version.

Syntax:

Array.sample[(count)]

The argument is optional.

Without arguments, It returns a single random value from an array. Arguments contain the number of elements to be returned from an array.

Here is an example

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]
# argument 2 returns the two random values
puts numberWords.sample(2)
# sample method without argument return one element from array
puts numberWords.sample

Output:

nine
two
five

It is always better to supply the argument less than the array size. It gives duplicate values if the argument is greater than the array size.

In the below example, the count is 11 and the array size is 10, so element five is repeated twice.

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]
# argument 11 returns the 11 random values
puts numberWords.sample(11)

Output:

ten
seven
six
nine
five
four
three
one
two
eight
five

Get Random values from an array shuffle

Array provides a shuffle method that shuffles the array of elements and returns the elements. It accepts optional arguments.

Syntax:

Array.shuffle() 
or 
Array.shuffle(random:range)

The argument is optional.

Without arguments, It returns a new array by shuffling all elements and returns it. Arguments contain the random range of values to be returned from an array.

Here is an example

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]

# shuffle method without argument returns a new array of elements with shuffled
puts numberWords.shuffle

Output:

three
ten
seven
five
one
four
two
eight
nine
six

with a range of random generator arguments,

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]
# argument random used random generator logic and return an array
puts numberWords.shuffle(random: Random.new(1))

Output:

three
ten
seven
five
one
four
two
eight
nine
six

if you want to return a single random value, You can use shuffle.first property.

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]
# returns single random value from an array
puts numberWords.shuffle.first

Output:

four

Random elementn from an array with a random index

rand method returns the number with a range of supplied values, if the array size is passed, It returns a random value between 0 to the length of an array, by passing this value as an index to retrieve the element.

Here is an example

numberWords = ["one", "two", "three", "four", "five", "six", "seven","eight","nine","ten" ]
# returns single random value from an array
puts numberWords[rand(numberWords.count)]

Output:

two

Conclusion

To summarize, Three options are listed to retrieve random values from an array. you can choose anything based on your need.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to Convert current Unix timestamp epoch to DateTime in Ruby Programming| Ruby on Rails by Example

How to remove an element from an array in Ruby Programming| Ruby on Rails by Example

How to check the type of a variable is Ruby| Ruby on Rails By Example

How to print the name of a Ruby class| Ruby on Rails By Example

How to check if the variable is defined in Ruby with examples

How to Find the minimum and maximum values in an array and hash Ruby with examples

How to Generate Random number in Ruby with examples