Commits

Timo Paulssen  committed d231d6f

added implementations for random_integers and randint with tests.

  • Participants
  • Parent commits a6ab5e7
  • Branches numpy-random

Comments (0)

Files changed (3)

File pypy/module/micronumpy/__init__.py

         "seed",
         "rand",
         "randn",
-        "standard_normal"]:
+        "standard_normal",
+        "random_integers",
+        "randint",
+    ]:
 
         appleveldefs[a] = "app_random.%s" % a
 

File pypy/module/micronumpy/app_random.py

 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)
+

File pypy/module/micronumpy/test/test_random.py

         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