Project

General

Profile

Actions

Bug #1117

closed

Array#choice always produces the same sequence

Added by stefanocr (Stefano Crocco) almost 16 years ago. Updated over 13 years ago.

Status:
Closed
Assignee:
-
ruby -v:
1.8.7-p72
[ruby-core:21880]

Description

=begin
In ruby 1.8.7-p72, Array#choice produces the same sequence of elements every time the script is run. For example:

ruby -e 'p [1,2,3,4].choice'
=> 4
ruby -e 'p [1,2,3,4].choice'
=> 4
ruby -e 'p [1,2,3,4].choice'
=> 4
ruby -e 'p [1,2,3,4].choice'
=> 4

According to the documentation, I'd expect this to give a different result every time. To achieve the result I expect, I need to call Kernel#srand before Array#choice:

ruby -e 'srand; p [1,2,3,4].choice'
=> 4
ruby -e 'p [1,2,3,4].choice'
=> 2
ruby -e 'p [1,2,3,4].choice'
=> 3
ruby -e 'p [1,2,3,4].choice'
=> 2
=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0