Bug #1117
closedArray#choice always produces the same sequence
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
Updated by shyouhei (Shyouhei Urabe) almost 16 years ago
- Status changed from Open to Closed
=begin
This issue has already been fixed in the repository.
=end
Updated by murphy (Kornelius Kalnbach) almost 16 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
Updated by matz (Yukihiro Matsumoto) almost 16 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 murphy@rubychan.de 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
Updated by murphy (Kornelius Kalnbach) almost 16 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