Skip to content

Commit 15f2cf5

Browse files
committed
Fix Array#sampleimplementation to be fair.
1 parent 8b5c8f7 commit 15f2cf5

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/main/ruby/truffleruby/core/array.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,11 +1073,12 @@ def sample(count = undefined, random: nil)
10731073
end
10741074

10751075
private def sample_many_swap(count, rng)
1076-
# linear dependence on array size, therefore very slow for small count / size
1076+
# linear dependence on count,
10771077
result = Array.new(self)
10781078

1079-
count.times do |c|
1080-
result.__send__ :swap, c, rng.rand(size)
1079+
count.times do |i|
1080+
r = i + rng.rand(result.size - i)
1081+
result.__send__ :swap, i, r
10811082
end
10821083

10831084
count == size ? result : result[0, count]

0 commit comments

Comments
 (0)