diff options
author | Robert Kern <robert.kern@gmail.com> | 2019-06-28 06:24:39 -0700 |
---|---|---|
committer | Matti Picus <matti.picus@gmail.com> | 2019-06-28 06:24:39 -0700 |
commit | 0ec7f12012bcb613857b3adef2b2d18310838894 (patch) | |
tree | 5bbba51d29b74750fae48722f0c52b5e873ca600 /numpy/random/tests | |
parent | defafbf73e18955e64130233d2b16320ae509641 (diff) | |
download | numpy-0ec7f12012bcb613857b3adef2b2d18310838894.tar.gz |
ENH: np.random.default_gen() (#13840)
* ENH: Rename seed_seq argument to seed and replace Generator() with default_gen()
Diffstat (limited to 'numpy/random/tests')
-rw-r--r-- | numpy/random/tests/test_direct.py | 18 | ||||
-rw-r--r-- | numpy/random/tests/test_smoke.py | 44 |
2 files changed, 61 insertions, 1 deletions
diff --git a/numpy/random/tests/test_direct.py b/numpy/random/tests/test_direct.py index 1e824fbcc..97cec6e26 100644 --- a/numpy/random/tests/test_direct.py +++ b/numpy/random/tests/test_direct.py @@ -7,7 +7,8 @@ from numpy.testing import (assert_equal, assert_allclose, assert_array_equal, import pytest from numpy.random import ( - Generator, MT19937, PCG64, Philox, RandomState, SeedSequence, SFC64 + Generator, MT19937, PCG64, Philox, RandomState, SeedSequence, SFC64, + default_gen ) from numpy.random.common import interface @@ -400,3 +401,18 @@ class TestSFC64(Base): cls.seed_error_type = (ValueError, TypeError) cls.invalid_init_types = [(3.2,), ([None],), (1, None)] cls.invalid_init_values = [(-1,)] + + +class TestDefaultGen(object): + def test_seed(self): + for args in [(), (None,), (1234,), ([1234, 5678],)]: + rg = default_gen(*args) + assert isinstance(rg.bit_generator, PCG64) + + def test_passthrough(self): + bg = Philox() + rg = default_gen(bg) + assert rg.bit_generator is bg + rg2 = default_gen(rg) + assert rg2 is rg + assert rg2.bit_generator is bg diff --git a/numpy/random/tests/test_smoke.py b/numpy/random/tests/test_smoke.py index 4263335f6..12feec098 100644 --- a/numpy/random/tests/test_smoke.py +++ b/numpy/random/tests/test_smoke.py @@ -764,6 +764,50 @@ class TestSFC64(RNG): cls._extra_setup() +class TestPCG64(RNG): + @classmethod + def setup_class(cls): + cls.bit_generator = PCG64 + cls.advance = 2**63 + 2**31 + 2**15 + 1 + cls.seed = [12345] + cls.rg = Generator(cls.bit_generator(*cls.seed)) + cls.initial_state = cls.rg.bit_generator.state + cls.seed_vector_bits = 64 + cls._extra_setup() + + +class TestDefaultGen(RNG): + @classmethod + def setup_class(cls): + # This will duplicate some tests that directly instantiate a fresh + # Generator(), but that's okay. + cls.bit_generator = PCG64 + cls.advance = 2**63 + 2**31 + 2**15 + 1 + cls.seed = [12345] + cls.rg = np.random.default_gen(*cls.seed) + cls.initial_state = cls.rg.bit_generator.state + cls.seed_vector_bits = 64 + cls._extra_setup() + + def test_default_is_pcg64(self): + # In order to change the default BitGenerator, we'll go through + # a deprecation cycle to move to a different function. + assert_(isinstance(self.rg.bit_generator, PCG64)) + + def test_seed(self): + np.random.default_gen() + np.random.default_gen(None) + np.random.default_gen(12345) + np.random.default_gen(0) + np.random.default_gen(43660444402423911716352051725018508569) + np.random.default_gen([43660444402423911716352051725018508569, + 279705150948142787361475340226491943209]) + with pytest.raises(ValueError): + np.random.default_gen(-1) + with pytest.raises(ValueError): + np.random.default_gen([12345, -1]) + + class TestEntropy(object): def test_entropy(self): e1 = entropy.random_entropy() |