# Array#choice always produce the same sequence

**URL:** <https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529>\
**Category:** Ruby\
**Created:** [February 5, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529 "2009-02-05T13:01:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![crocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@crocco](https://www.ruby-forum.com/u/crocco)\
**Post date:** [February 5, 2009, 1:01pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529/1 "2009-02-05T13:01:52Z")

</div>

I’ve just stumbled upon a strange behaviour of Array#choice (using ruby  
1.8.7-  
p72). As far I understand, an\_array.choice should be (almost) the same  
as  
an\_array[rand(an\_array.size)]. Thus, the two following pieces of code  
should  
be equivalent

a = [1, 2, 3, 4]  
p 10.times.map{a.choice}.join  
p 10.times.map{a[rand(a.size)].join}

However, this doesn’t seem to be the case. In particular, something  
like:

ruby -e ‘a = [1, 2, 3, 4]; p 10.times.map{a.choice}.join’

always give the same result, while

---

<div class="post-metadata">

**Author:** ![crocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@crocco](https://www.ruby-forum.com/u/crocco)\
**Post date:** [February 5, 2009, 1:11pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529/2 "2009-02-05T13:11:51Z")

</div>

Alle Thursday 05 February 2009, Stefano C. ha scritto:

> ruby -e ‘a = [1, 2, 3, 4]; p 10.times.map{a.choice}.join’
> 
> always give the same result, while

Sorry, hit the “Send” button too soon. Here’s the full version

I’ve just stumbled upon a strange behaviour of Array#choice (using ruby  
1.8.7- p72). As far I understand, an\_array.choice should be (almost) the  
same as an\_array[rand(an\_array.size)]. Thus, the two following pieces of  
code should be equivalent

a = [1, 2, 3, 4]  
p 10.times.map{a.choice}.join  
p 10.times.map{a[rand(a.size)]}.join

However, this doesn’t seem to be the case. In particular, something  
like:

ruby -e ‘a = [1, 2, 3, 4]; p 10.times.map{a.choice}.join’

always give the same result, while

ruby -e ‘a = [1, 2, 3, 4]; p 10.times.map{a[rand(a.size)]}.join’

gives a different result every time the command is executed (which, in  
my  
opinion, is the correct behaviour).

It seems that to get the correct behaviour from choice, you need to call  
srand  
before using it. Does anyone know why this is necessary with choice but  
not  
with rand? Is it the intended behaviour or a bug?

Thanks

Stefano

---

<div class="post-metadata">

**Author:** ![crocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@crocco](https://www.ruby-forum.com/u/crocco)\
**Post date:** [February 5, 2009, 1:26pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529/3 "2009-02-05T13:26:31Z")

</div>

Hi Stefano,

it seems to work for me:

irb(main):003:0\> a = [1, 2, 3, 4]  
=\> [1, 2, 3, 4]  
irb(main):004:0\> p 10.times.map{a.choice}.join  
“4414312344”  
=\> nil  
irb(main):006:0\> p 10.times.map{a[rand(a.size)]}.join  
“4342411331”  
=\> nil  
irb(main):007:0\> p 10.times.map{a.choice}.join  
“2333341212”  
=\> nil  
irb(main):008:0\> p 10.times.map{a.choice}.join  
“1312441122”  
=\> nil  
irb(main):009:0\> p 10.times.map{a.choice}.join  
“2412444223”  
=\> nil  
Z ruby -v  
ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

---

<div class="post-metadata">

**Author:** ![crocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@crocco](https://www.ruby-forum.com/u/crocco)\
**Post date:** [February 5, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529/4 "2009-02-05T18:12:43Z")

</div>

On Thu, Feb 5, 2009 at 2:42 PM, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (without arguments), I get the correct behaviour, that is a different number  
> every time the script is run.

It looks like it was fixed in ruby1.9 (notice that #choice has been  
renamed to #sample):

ruby1.9.1 -e “p [1,2,3,4].sample”  
4  
ruby1.9.1 -e “p [1,2,3,4].sample”  
1  
ruby1.9.1 -e “p [1,2,3,4].sample”  
2  
ruby1.9.1 -e “p [1,2,3,4].sample”  
3  
ruby1.9.1 -e “p [1,2,3,4].sample”  
1  
ruby1.9.1 -e “p [1,2,3,4].sample”

File a bug or ask in ruby-core if it’s desired behavior.

–  
Pozdrawiam

Rados³aw Bu³at  
[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![crocco](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/fbc32d/32.png) [@crocco](https://www.ruby-forum.com/u/crocco)\
**Post date:** [February 5, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/array-choice-always-produce-the-same-sequence/159529/5 "2009-02-05T14:43:08Z")

</div>

Alle Thursday 05 February 2009, ruud grosmann ha scritto:

> > srand  
> > before using it. Does anyone know why this is necessary with choice but  
> > not with rand? Is it the intended behaviour or a bug?
> > 
> > Thanks
> > 
> > Stefano

> “4342411331”  
> Z ruby -v  
> ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]

Maybe I wasn’t clear in explaining the issue. Within a single irb  
session (or  
ruby invocation in general) I, too, get different results at each  
invocation.  
The problem is that if I run the script twice, I get the same results  
for each  
call to choice (that is, the first call to choice gives the same result  
at  
each ruby invocation; the second call always gives the same result,  
which can  
be different from the first, and so on). A simpler example:

File: test.rb

p [1,2,3,4].choice

ruby test.rb  
=\> 4  
ruby test.rb  
=\> 4  
ruby test.rb  
=\> 4

And so on. If at the beginning of the script I add a call to  
Kernel#srand  
(without arguments), I get the correct behaviour, that is a different  
number  
every time the script is run.

Stefano
