Project

General

Profile

Actions

Backport #7935

closed

Array#sample with random generator

Added by marcandre (Marc-Andre Lafortune) almost 12 years ago. Updated almost 12 years ago.


Description

The Random generator method's rand is called with the wrong limit.

Kernel::rand(n) returns integer between 0 and n-1, but generator is called with (n-1).

For example:

require 'delegate'
[1, 2].sample(1, random: Random.new) # => [1], or [2]
[1, 2].sample(1, random: SimpleDelegator.new(Random.new)) # => [1], never [2]

test_random_ulong_limited

Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 12 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r39466.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


random.c: increase limit for generic rand

  • random.c (rb_random_ulong_limited): limit is inclusive, but generic
    rand method should return a number less than it, so increase for the
    difference. [ruby-core:52779] [Bug #7935]
Actions #2

Updated by nobu (Nobuyoshi Nakada) almost 12 years ago

  • Tracker changed from Bug to Backport
  • Project changed from Ruby master to Backport200
  • Category deleted (core)
  • Status changed from Closed to Assigned
  • Assignee set to mame (Yusuke Endoh)
  • Target version deleted (2.6)

Updated by nagachika (Tomoyuki Chikanaga) almost 12 years ago

  • Assignee changed from mame (Yusuke Endoh) to nagachika (Tomoyuki Chikanaga)
Actions #4

Updated by nagachika (Tomoyuki Chikanaga) almost 12 years ago

  • Status changed from Assigned to Closed

This issue was solved with changeset r39675.
Marc-Andre, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


merge revision(s) 39466,39470: [Backport #7935]

* random.c (rb_random_ulong_limited): limit is inclusive, but generic
  rand method should return a number less than it, so increase for the
  difference.  [ruby-core:52779] [Bug #7935]

* test/ruby/test_array.rb (test_sample_random): remove adjustment for
  the bug fixed by r39466.  [ruby-core:52779] [Bug #7935]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0