Пример #1
0
def test_PythonRandomInterface():
    np = pytest.importorskip('numpy')
    rs = np.random.RandomState
    rng = PythonRandomInterface(rs(42))
    rs42 = rs(42)

    # make sure these functions are same as expected outcome
    assert rng.randrange(3, 5) == rs42.randint(3, 5)
    assert np.all(rng.choice([1, 2, 3]) == rs42.choice([1, 2, 3]))
    assert rng.gauss(0, 1) == rs42.normal(0, 1)
    assert rng.expovariate(1.5) == rs42.exponential(1/1.5)
    assert np.all(rng.shuffle([1, 2, 3]) == rs42.shuffle([1, 2, 3]))
    assert np.all(rng.sample([1, 2, 3], 2) ==
                  rs42.choice([1, 2, 3], (2,), replace=False))
    assert rng.randint(3, 5) == rs42.randint(3, 6)
    assert rng.random() == rs42.random_sample()
Пример #2
0
def test_create_py_random_state():
    pyrs = random.Random

    assert isinstance(create_py_random_state(1), pyrs)
    assert isinstance(create_py_random_state(None), pyrs)
    assert isinstance(create_py_random_state(pyrs(1)), pyrs)
    pytest.raises(ValueError, create_py_random_state, 'a')

    np = pytest.importorskip('numpy')

    rs = np.random.RandomState
    nprs = PythonRandomInterface
    assert isinstance(create_py_random_state(np.random), nprs)
    assert isinstance(create_py_random_state(rs(1)), nprs)
    # test default rng input
    assert isinstance(PythonRandomInterface(), nprs)