test_random.py 2.72 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

18
19
20
21
22
23
import numpy as np

import nifty6 as ift


def test_rand1():
24
25
26
27
    with ift.random.Context(31):
        a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(31):
        b = ift.random.current_rng().integers(0,1000000000)
28
29
30
31
    np.testing.assert_equal(a,b)


def test_rand2():
Martin Reinecke's avatar
Martin Reinecke committed
32
33
34
35
36
    sseq = ift.random.spawn_sseq(10)
    with ift.random.Context(sseq[2]):
        a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(sseq[2]):
        b = ift.random.current_rng().integers(0,1000000000)
37
38
39
40
    np.testing.assert_equal(a,b)


def test_rand3():
41
42
43
44
45
46
47
48
49
50
    with ift.random.Context(31):
        sseq = ift.random.spawn_sseq(10)
        with ift.random.Context(sseq[2]):
            a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(31):
        sseq = ift.random.spawn_sseq(1)
        sseq = ift.random.spawn_sseq(1)
        sseq = ift.random.spawn_sseq(1)
        with ift.random.Context(sseq[0]):
            b = ift.random.current_rng().integers(0,1000000000)
51
52
53
54
    np.testing.assert_equal(a,b)


def test_rand4():
55
56
57
58
59
60
61
    with ift.random.Context(31):
        a = ift.random.current_rng().integers(0,1000000000)
        with ift.random.Context(31):
            b = ift.random.current_rng().integers(0,1000000000)
    np.testing.assert_equal(a,b)


62
63
64
65
66
67
68
69
70
71
72
def test_rand5():
    ift.random.push_sseq_from_seed(31)
    a = ift.random.current_rng().integers(0,1000000000)
    ift.random.push_sseq_from_seed(31)
    b = ift.random.current_rng().integers(0,1000000000)
    c = ift.random.current_rng().integers(0,1000000000)
    ift.random.pop_sseq()
    d = ift.random.current_rng().integers(0,1000000000)
    ift.random.pop_sseq()
    np.testing.assert_equal(a,b)
    np.testing.assert_equal(c,d)
73
74
75
76
77
78
79
80

def test_rand6():
    ift.random.push_sseq_from_seed(31)
    state = ift.random.getState()
    a = ift.random.current_rng().integers(0,1000000000)
    ift.random.setState(state)
    b = ift.random.current_rng().integers(0,1000000000)
    np.testing.assert_equal(a,b)
Martin Reinecke's avatar
Martin Reinecke committed
81
    ift.random.pop_sseq()
82