Пример #1
0
    def test_basics(self, rng_ctor):
        random = RandomStream(seed=utt.fetch_seed(), rng_ctor=rng_ctor)

        with pytest.raises(ValueError):
            random.uniform(0, 1, size=(2, 2), rng=np.random.default_rng(23))

        with pytest.raises(AttributeError):
            random.blah

        assert hasattr(random, "standard_normal")

        with pytest.raises(AttributeError):
            np_random = RandomStream(namespace=np, rng_ctor=rng_ctor)
            np_random.ndarray

        fn = function([],
                      random.uniform(0, 1, size=(2, 2)),
                      updates=random.updates())

        fn_val0 = fn()
        fn_val1 = fn()

        rng_seed = np.random.SeedSequence(utt.fetch_seed())
        (rng_seed, ) = rng_seed.spawn(1)
        rng = random.rng_ctor(rng_seed)

        numpy_val0 = rng.uniform(0, 1, size=(2, 2))
        numpy_val1 = rng.uniform(0, 1, size=(2, 2))

        assert np.allclose(fn_val0, numpy_val0)
        assert np.allclose(fn_val1, numpy_val1)
Пример #2
0
    def test_uniform(self, rng_ctor):
        # Test that RandomStream.uniform generates the same results as numpy
        # Check over two calls to see if the random state is correctly updated.
        random = RandomStream(utt.fetch_seed(), rng_ctor=rng_ctor)
        fn = function([], random.uniform(-1, 1, size=(2, 2)))
        fn_val0 = fn()
        fn_val1 = fn()

        rng_seed = np.random.SeedSequence(utt.fetch_seed())
        (rng_seed, ) = rng_seed.spawn(1)

        rng = random.rng_ctor(rng_seed)
        numpy_val0 = rng.uniform(-1, 1, size=(2, 2))
        numpy_val1 = rng.uniform(-1, 1, size=(2, 2))

        assert np.allclose(fn_val0, numpy_val0)
        assert np.allclose(fn_val1, numpy_val1)
Пример #3
0
    def test_seed(self, rng_ctor):
        init_seed = 234
        random = RandomStream(init_seed, rng_ctor=rng_ctor)

        assert random.default_instance_seed == init_seed

        new_seed = 43298
        random.seed(new_seed)

        rng_seed = np.random.SeedSequence(new_seed)
        assert random.gen_seedgen.entropy == rng_seed.entropy

        random.seed()

        rng_seed = np.random.SeedSequence(init_seed)
        assert random.gen_seedgen.entropy == rng_seed.entropy

        # Reset the seed
        random.seed(new_seed)

        # Check state updates
        _ = random.normal()

        # Now, change the seed when there are state updates
        random.seed(new_seed)

        update_seed = np.random.SeedSequence(new_seed)
        (update_seed, ) = update_seed.spawn(1)
        ref_rng = random.rng_ctor(update_seed)
        state_rng = random.state_updates[0][0].get_value(borrow=True)

        if hasattr(state_rng, "get_state"):
            ref_state = ref_rng.get_state()
            random_state = state_rng.get_state()
            assert np.array_equal(random_state[1], ref_state[1])
            assert random_state[0] == ref_state[0]
            assert random_state[2:] == ref_state[2:]
        else:
            ref_state = ref_rng.__getstate__()
            random_state = state_rng.__getstate__()
            assert random_state["bit_generator"] == ref_state["bit_generator"]
            assert random_state["state"] == ref_state["state"]