mirror of
https://github.com/ruby/ruby.git
synced 2025-09-16 09:04:05 +02:00
* array.c (rb_ary_shuffle_bang, rb_ary_sample): add optional
argument random. [ruby-dev:41923] [EXPERIMENTAL] * random.c (rb_random_{int32,real,bytes}): fallback to normal method invocation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
89339af9c1
commit
2f6c0e3be3
5 changed files with 88 additions and 18 deletions
|
@ -1891,6 +1891,12 @@ class TestArray < Test::Unit::TestCase
|
|||
100.times do
|
||||
assert_equal([0, 1, 2], [2, 1, 0].shuffle.sort)
|
||||
end
|
||||
|
||||
gen = Random.new(0)
|
||||
srand(0)
|
||||
100.times do
|
||||
assert_equal([0, 1, 2].shuffle, [0, 1, 2].shuffle(random: gen))
|
||||
end
|
||||
end
|
||||
|
||||
def test_sample
|
||||
|
@ -1907,7 +1913,7 @@ class TestArray < Test::Unit::TestCase
|
|||
(0..20).each do |n|
|
||||
100.times do
|
||||
b = a.sample(n)
|
||||
assert_equal([n, 18].min, b.uniq.size)
|
||||
assert_equal([n, 18].min, b.size)
|
||||
assert_equal(a, (a | b).sort)
|
||||
assert_equal(b.sort, (a & b).sort)
|
||||
end
|
||||
|
@ -1920,6 +1926,15 @@ class TestArray < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
assert_raise(ArgumentError, '[ruby-core:23374]') {[1, 2].sample(-1)}
|
||||
|
||||
gen = Random.new(0)
|
||||
srand(0)
|
||||
a = (1..18).to_a
|
||||
(0..20).each do |n|
|
||||
100.times do |i|
|
||||
assert_equal(a.sample(n), a.sample(n, random: gen), "#{i}/#{n}")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_cycle
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue