Project

General

Profile

Actions

Bug #1117

closed

Array#choice always produces the same sequence

Added by stefanocr (Stefano Crocco) about 15 years ago. Updated about 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 #1

Updated by shyouhei (Shyouhei Urabe) about 15 years ago

  • Status changed from Open to Closed

=begin
This issue has already been fixed in the repository.
=end

Actions #2

Updated by murphy (Kornelius Kalnbach) about 15 years ago

=begin
Stefano Crocco wrote:

Bug #1117: Array#choice always produces the same sequence
actually, why isn't srand initialized with Time.now.to_i or just 0 by
default? we had the issue with Array#shuffle before...

[murphy]

=end

Actions #3

Updated by matz (Yukihiro Matsumoto) about 15 years ago

=begin
Hi,

In message "Re: [ruby-core:21882] Re: [Bug #1117] Array#choice always produces the same sequence"
on Fri, 6 Feb 2009 17:36:13 +0900, Kornelius Kalnbach writes:

|> Bug #1117: Array#choice always produces the same sequence
|actually, why isn't srand initialized with Time.now.to_i or just 0 by
|default? we had the issue with Array#shuffle before...

Don't worry, it is now. Those two problems were fixed at the same
time.

						matz.

=end

Actions #4

Updated by murphy (Kornelius Kalnbach) about 15 years ago

=begin
Yukihiro Matsumoto wrote:

Don't worry, it is now. Those two problems were fixed at the same
time.
I'm sorry :)

it also seems Array#choice was replaced by the nicer #sample. didn't
know that.

[murphy]

=end

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0