Exemplo n.º 1
0
    def test_seed(self):
        init_seed = 234
        random = RandomStream(init_seed)

        ref_state = np.random.RandomState(init_seed).get_state()
        random_state = random.gen_seedgen.get_state()
        assert random.default_instance_seed == init_seed
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]

        new_seed = 43298
        random.seed(new_seed)

        ref_state = np.random.RandomState(new_seed).get_state()
        random_state = random.gen_seedgen.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:]

        random.seed()
        ref_state = np.random.RandomState(init_seed).get_state()
        random_state = random.gen_seedgen.get_state()
        assert random.default_instance_seed == init_seed
        assert np.array_equal(random_state[1], ref_state[1])
        assert random_state[0] == ref_state[0]
        assert random_state[2:] == ref_state[2:]

        # 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)

        rng = np.random.RandomState(new_seed)
        update_seed = rng.randint(2**30)
        ref_state = np.random.RandomState(update_seed).get_state()
        random_state = random.state_updates[0][0].get_value(
            borrow=True).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:]
Exemplo n.º 2
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"]