Author: Timo Paulssen <timona...@perpetuum-immobile.de> Branch: numpy-random Changeset: r47586:c059753d0865 Date: 2011-09-24 20:30 +0200 http://bitbucket.org/pypy/pypy/changeset/c059753d0865/
Log: implement a couple integer random methods, too. diff --git a/lib_pypy/numpy/random.py b/lib_pypy/numpy/random.py --- a/lib_pypy/numpy/random.py +++ b/lib_pypy/numpy/random.py @@ -29,3 +29,31 @@ def standard_normal(size=None): return randn(*size) +def random_integers(low, high=None, size=None): + print "random_integers called with %s, %s" % (low, high) + + if high is None: + low, high = 1, low + else: + low, high = low, high + + print "values are now %s, %s"% (low, high) + + if size is None: + return _random.randint(low, high) + else: + assert len(size) == 1 + + return array(_random.randint(low, high) for x in range(size[0])) + +def randint(low, high=None, size=None): + print "randint called with %s, %s"% (low, high) + if high is None: + low, high = 0, low - 1 + else: + low, high = low, high - 1 + + print "values are now %s, %s"% (low, high) + + return random_integers(low, high, size) + diff --git a/lib_pypy/pypy_test/test_numpy_random.py b/lib_pypy/pypy_test/test_numpy_random.py --- a/lib_pypy/pypy_test/test_numpy_random.py +++ b/lib_pypy/pypy_test/test_numpy_random.py @@ -61,3 +61,52 @@ seed(9001) assert number == rand(1)[0] assert other_number == rand(1)[0] + + def test_randint_single(self): + from numpy.random import randint + + for i in range(100): + integer = randint(4) + assert isinstance(integer, int) + assert 0 <= integer < 4 + + for i in range(100): + integer = randint(9, 12) + assert isinstance(integer, int) + assert 9 <= integer < 12 + + def test_randint_multi(self): + from numpy.random import randint + + integers = randint(4, size=(100,)) + assert integers.shape == (100,) + for x in integers: + assert 0 <= x < 4 + + integers = randint(9, 12, (100,)) + for x in integers: + assert 9 <= x < 12 + + def test_random_integers_single(self): + from numpy.random import random_integers + + for i in range(100): + integer = random_integers(4) + assert 0 <= integer <= 4 + + for i in range(100): + integer = random_integers(9, 12) + assert 9 <= integer <= 12 + + def test_random_integers_multi(self): + from numpy.random import random_integers + + integers = random_integers(5, size=(100,)) + assert integers.shape == (100,) + for x in integers: + assert 0 <= integers[x] <= 5 + + integers = random_integers(9, 12, (100,)) + assert integers.shape == (100,) + for x in integers: + assert 9 <= x <= 12 _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit