# Need help randomly selecting unique values from an array

**URL:** <https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396>\
**Category:** Ruby\
**Created:** [August 30, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396 "2007-08-30T18:22:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [August 30, 2007, 6:22pm UTC](https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396/1 "2007-08-30T18:22:57Z")

</div>

Hi there,

I have a simple array that I have to pick one or more unique values  
from and am kind of stumped as to how to do this. Here’s the what the  
array looks like:

foo = [2, 7, 10, 14] # (or could equal a diff’t set of 4  
numbers)

Now for this task, I’m not interested in the first element at all.  
I’d rather just leave it alone and not delete it though.  
What I need to do is pick 1 to 3 elements randomly from the array. I  
can’t reuse the numbers.

So, for example, if I need 1 number then it could be any of the  
elements 1-3 from the array.  
If I need 2 numbers, it could be any of elements 1-3 but I can’t pick  
the same number twice. Ditto for when I need 3 numbers.

I don’t need the numbers in any particular order, so I’m just looking  
for the simplest loop/function to pick the unique numbers for me.

Any suggestions? Thanks.

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [August 30, 2007, 6:33pm UTC](https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396/2 "2007-08-30T18:33:10Z")

</div>

You could port this code over from C…

> **[Random Number](https://xkcd.com/221/)**
>
> RFC 1149.5 specifies 4 as the standard IEEE-vetted random number.

OK ok seriously though.  
rand(foo.length) will randomly pick any one of them, but you want to  
ignore index zero so  
rand(foo.length -1 ) + 1 should do the trick.

This is a slightly… stupid way of doing it, but considering the size  
of the data, it’s a choice of what’s more stupid, generating random  
numbers until you get two that aren’t the same, or something like  
this…

number1 = foo[rand(foo.length-1)+1]  
foo2=foo-[number1]  
number2 = foo2[rand(foo2.length-1)+1]

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [August 30, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396/3 "2007-08-30T18:35:11Z")

</div>

Paul wrote:

> foo = [2, 7, 10, 14] # (or could equal a diff’t set of 4  
> numbers)
> 
> Now for this task, I’m not interested in the first element at all.  
> I’d rather just leave it alone and not delete it though.

So you use foo[1…-1]. That will create a new array not containing the  
first  
item and leave the original array untouched.

> What I need to do is pick 1 to 3 elements randomly from the array. I  
> can’t reuse the numbers.

foo[1…-1].sort\_by {rand}.first(n)  
where n is a number between 1 and 3.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![paul](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@paul](https://www.ruby-forum.com/u/paul)\
**Post date:** [August 30, 2007, 7:31pm UTC](https://www.ruby-forum.com/t/need-help-randomly-selecting-unique-values-from-an-array/113396/4 "2007-08-30T19:31:34Z")

</div>

Excellent! That’s amazingly simple! (and I never would have figured  
that out)  
It’s just what I need. Thanks.
