[pypy-commit] pypy numpy-random: added implementations for random_integers and randint with tests.

timo_jbo noreply at buildbot.pypy.org
Sat Sep 24 20:31:22 CEST 2011


Author: Timo Paulssen <timonator at perpetuum-immobile.de>
Branch: numpy-random
Changeset: r47584:d231d6fbeb81
Date: 2011-09-22 20:40 +0200
http://bitbucket.org/pypy/pypy/changeset/d231d6fbeb81/

Log:	added implementations for random_integers and randint with tests.

diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -13,7 +13,10 @@
         "seed",
         "rand",
         "randn",
-        "standard_normal"]:
+        "standard_normal",
+        "random_integers",
+        "randint",
+    ]:
 
         appleveldefs[a] = "app_random.%s" % a
 
diff --git a/pypy/module/micronumpy/app_random.py b/pypy/module/micronumpy/app_random.py
--- a/pypy/module/micronumpy/app_random.py
+++ b/pypy/module/micronumpy/app_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/pypy/module/micronumpy/test/test_random.py b/pypy/module/micronumpy/test/test_random.py
--- a/pypy/module/micronumpy/test/test_random.py
+++ b/pypy/module/micronumpy/test/test_random.py
@@ -58,3 +58,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


More information about the pypy-commit mailing list