We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Array#sample
1 parent 8b5c8f7 commit 15f2cf5Copy full SHA for 15f2cf5
src/main/ruby/truffleruby/core/array.rb
@@ -1073,11 +1073,12 @@ def sample(count = undefined, random: nil)
1073
end
1074
1075
private def sample_many_swap(count, rng)
1076
- # linear dependence on array size, therefore very slow for small count / size
+ # linear dependence on count,
1077
result = Array.new(self)
1078
1079
- count.times do |c|
1080
- result.__send__ :swap, c, rng.rand(size)
+ count.times do |i|
+ r = i + rng.rand(result.size - i)
1081
+ result.__send__ :swap, i, r
1082
1083
1084
count == size ? result : result[0, count]
0 commit comments