Skip to content

Commit deabe67

Browse files
committedDec 28, 2015
Array#sample checks all indexes when spinning. Fixes #3506.
1 parent f0b5ce6 commit deabe67

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed
 

‎kernel/common/array.rb

+19-9
Original file line numberDiff line numberDiff line change
@@ -1365,28 +1365,38 @@ def sample(count=undefined, options=undefined)
13651365
else
13661366
if size / count > 3
13671367
abandon = false
1368-
spin = 0
13691368

13701369
result = Array.new count
13711370
i = 1
13721371

13731372
result[0] = rng.rand(size)
13741373
while i < count
13751374
k = rng.rand(size)
1376-
j = 0
13771375

1378-
while j < i
1379-
while k == result[j]
1380-
spin += 1
1381-
if spin > 100
1376+
spin = false
1377+
spin_count = 0
1378+
1379+
while true
1380+
j = 0
1381+
while j < i
1382+
if k == result[j]
1383+
spin = true
1384+
break
1385+
end
1386+
1387+
j += 1
1388+
end
1389+
1390+
if spin
1391+
if (spin_count += 1) > 100
13821392
abandon = true
13831393
break
13841394
end
1395+
13851396
k = rng.rand(size)
1397+
else
1398+
break
13861399
end
1387-
break if abandon
1388-
1389-
j += 1
13901400
end
13911401

13921402
break if abandon

0 commit comments

Comments
 (0)
Please sign in to comment.