Project

General

Profile

Backport #7935

Array#sample with random generator

Added by marcandre (Marc-Andre Lafortune) over 6 years ago. Updated over 6 years ago.

Status:
Closed
Priority:
Normal
[ruby-core:52779]

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

Associated revisions

Revision d64c926e
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39466 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 39466
Added by nobu (Nobuyoshi Nakada) over 6 years ago

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]

Revision 9e068352
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@39470 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 39470
Added by nobu (Nobuyoshi Nakada) over 6 years ago

test_array.rb: fix test for r39466

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

Revision 0c2b3f7f
Added by nagachika (Tomoyuki Chikanaga) over 6 years ago

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]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_0_0@39675 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 39675
Added by nagachika (Tomoyuki Chikanaga) over 6 years ago

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]

History

#1

Updated by nobu (Nobuyoshi Nakada) over 6 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]
#2

Updated by nobu (Nobuyoshi Nakada) over 6 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) over 6 years ago

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

Updated by nagachika (Tomoyuki Chikanaga) over 6 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]

Also available in: Atom PDF