Esempio n. 1
0
def test_random_state():
    import numpy.random as npr

    # Check with seed
    state = com.random_state(5)
    assert state.uniform() == npr.RandomState(5).uniform()

    # Check with random state object
    state2 = npr.RandomState(10)
    assert com.random_state(state2).uniform() == npr.RandomState(10).uniform()

    # check with no arg random state
    assert com.random_state() is np.random

    # check array-like
    # GH32503
    state_arr_like = npr.randint(0, 2 ** 31, size=624, dtype="uint32")
    assert (
        com.random_state(state_arr_like).uniform()
        == npr.RandomState(state_arr_like).uniform()
    )

    # Check BitGenerators
    # GH32503
    if not np_version_under1p17:
        assert (
            com.random_state(npr.MT19937(3)).uniform()
            == npr.RandomState(npr.MT19937(3)).uniform()
        )
        assert (
            com.random_state(npr.PCG64(11)).uniform()
            == npr.RandomState(npr.PCG64(11)).uniform()
        )

    # Error for floats or strings
    msg = (
        "random_state must be an integer, array-like, a BitGenerator, "
        "a numpy RandomState, or None"
    )
    with pytest.raises(ValueError, match=msg):
        com.random_state("test")

    with pytest.raises(ValueError, match=msg):
        com.random_state(5.5)
Esempio n. 2
0
def test_randomfunc_parallel_pcg64():
    mesh = UnitSquareMesh(10, 10)
    V0 = VectorFunctionSpace(mesh, "CG", 1)
    V1 = FunctionSpace(mesh, "CG", 1)
    V = V0 * V1

    seed = 123456789
    # Original
    bgen = randomgen.PCG64(seed=seed)
    state = bgen.state
    state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
    bgen.state = state
    rg_base = randomgen.Generator(bgen)
    # Firedrake wrapper
    fgen = randomfunctiongen.PCG64(seed=seed)
    state = fgen.state
    state['state'] = {'state': seed, 'inc': V.comm.Get_rank()}
    fgen.state = state
    rg_wrap = randomfunctiongen.Generator(fgen)
    for i in range(1, 10):
        f = rg_wrap.beta(V, 0.3, 0.5)
        with f.dat.vec_ro as v:
            assert np.allclose(rg_base.beta(0.3, 0.5, size=(v.local_size,)), v.array[:])
Esempio n. 3
0
def get_gen(seed: Optional[int]) -> nrd.Generator:
    return nrd.PCG64(seed)