# Randomly select an element from an array

**URL:** <https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185>\
**Category:** Ruby\
**Created:** [June 22, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185 "2006-06-22T21:46:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 22, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/1 "2006-06-22T21:46:21Z")

</div>

I have an array:

[1,2,3,4,5]

How do I randomly select an element from the array?

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 22, 2006, 9:55pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/2 "2006-06-22T21:55:01Z")

</div>

On Jun 22, 2006, at 21:46, David S. wrote:

> I have an array:
> 
> [1,2,3,4,5]
> 
> How do I randomly select an element from the array?

irb(main):001:0\> a = %w(foo bar baz)  
=\> [“foo”, “bar”, “baz”]  
irb(main):002:0\> a[rand(a.length)]  
=\> “bar”

– fxn

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 22, 2006, 10:04pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/3 "2006-06-22T22:04:20Z")

</div>

On Jun 22, 2006, at 2:46 PM, David S. wrote:

> I have an array:
> 
> [1,2,3,4,5]
> 
> How do I randomly select an element from the array?

You ask the entire ruby-talk mailing list: the answer is 4.  
(the next is 2.)

class Array  
def rand  
self[Kernel.rand(size)]  
end  
end

[1,2,3,4,5].rand

jeremy

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 22, 2006, 10:22pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/4 "2006-06-22T22:22:16Z")

</div>

David S. wrote:

> I have an array:
> 
> [1,2,3,4,5]
> 
> How do I randomly select an element from the array?

Array now has built-in random sorting so you could

[1,2,3,4,5].sort\_by{rand}.last

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 23, 2006, 10:50am UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/5 "2006-06-23T10:50:49Z")

</div>

2006/6/22, Chris H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> David S. wrote:
> 
> > I have an array:
> > 
> > [1,2,3,4,5]
> > 
> > How do I randomly select an element from the array?
> 
> Array now has built-in random sorting so you could
> 
> [1,2,3,4,5].sort\_by{rand}.last

While this certainly works it’s extremely inefficient.

robert

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [June 23, 2006, 12:08pm UTC](https://www.ruby-forum.com/t/randomly-select-an-element-from-an-array/64185/6 "2006-06-23T12:08:42Z")

</div>

Robert K. schrieb:

> > [1,2,3,4,5].sort\_by{rand}.last
> 
> While this certainly works it’s extremely inefficient.
> 
> robert

solution 1:

a = [1,2,3,4,5]  
puts a[rand(a.size)]

solution 2:

class Array  
def random\_element  
self[rand(self.size)]  
end  
end

puts [1,2,3,4,5].random\_element
