Skip to content

Commit

Permalink
Array#sample checks all indexes when spinning. Fixes #3506.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Dec 28, 2015
1 parent f0b5ce6 commit deabe67
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions kernel/common/array.rb
Expand Up @@ -1365,28 +1365,38 @@ def sample(count=undefined, options=undefined)
else
if size / count > 3
abandon = false
spin = 0

result = Array.new count
i = 1

result[0] = rng.rand(size)
while i < count
k = rng.rand(size)
j = 0

while j < i
while k == result[j]
spin += 1
if spin > 100
spin = false
spin_count = 0

while true
j = 0
while j < i
if k == result[j]
spin = true
break
end

j += 1
end

if spin
if (spin_count += 1) > 100
abandon = true
break
end

k = rng.rand(size)
else
break
end
break if abandon

j += 1
end

break if abandon
Expand Down

0 comments on commit deabe67

Please sign in to comment.