Repository: numba/numba

Issue:
np.random.random_sample(int64,) with NumPy 1.17/Numba 0.45.1
<!--

Thanks for opening an issue! To help the Numba team handle your information
efficiently, please first ensure that there is no other issue present that
already describes the issue you have
(search at https://github.com/numba/numba/issues?&q=is%3Aissue).

For more general "how do I do X?" type questions, please speak to us in real
time on https://gitter.im/numba/numba or post to the Numba mailing list
https://groups.google.com/a/continuum.io/forum/#!forum/numba-users.

-->

## Reporting a bug

<!--

Before submitting a bug report please ensure that you can check off these boxes:

-->

- [x] I am using the latest released version of Numba (most recent is visible in
 the change log (https://github.com/numba/numba/blob/master/CHANGE_LOG).
- [x] I have included below a minimal working reproducer (if you are unsure how
 to write one see http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports).

<!--

Please include details of the bug here, including, if applicable, what you
expected to happen!

-->

---

Despite PR #4371, it seems that `np.random.random_sample(size)` does not work with NumPy 1.17.0 (while `np.random.random_sample()` does work) in nopython mode.

Does not work:

```py
@njit
def f0(size):
    return np.random.random_sample(size)
```

```py
f0(3)
```

```python-traceback
...

NotImplementedError: No definition for lowering np.random.random_sample(int64,) -> array(float64, 1d, C)

...

numba.errors.LoweringError: Failed in nopython mode pipeline (step: nopython mode backend)
No definition for lowering np.random.random_sample(int64,) -> array(float64, 1d, C)

...
```

Works:

```py
@njit
def f1(size):
    return np.random.random(size)
```

```py
f1(3)
```

```
array([0.97427529, 0.46575848, 0.10524335])
```

Works:

```py
@njit
def f2(size):
    return np.random.random_sample()
```

```py
f2(3)
```

```
0.5291699027196753
```

```py
>>> np.__version__
'1.17.0'
>>> numba.__version__
'0.45.1'
```


Discussion:

Resolving patch:
diff --git a/numba/targets/randomimpl.py b/numba/targets/randomimpl.py
--- a/numba/targets/randomimpl.py
+++ b/numba/targets/randomimpl.py
@@ -1259,6 +1259,9 @@ def permutation_impl(x):
     ("np.random.poisson", 2),
     ("np.random.power", 2),
     ("np.random.random", 1),
+    ("np.random.random_sample", 1),
+    ("np.random.ranf", 1),
+    ("np.random.sample", 1),
     ("np.random.randint", 3),
     ("np.random.rayleigh", 2),
     ("np.random.standard_cauchy", 1),


Test patch:
diff --git a/numba/tests/test_random.py b/numba/tests/test_random.py
--- a/numba/tests/test_random.py
+++ b/numba/tests/test_random.py
@@ -262,6 +262,22 @@ def test_numpy_random(self):
         self._check_random_seed(numpy_seed, jit_nullary("np.random.sample"))
         self._check_random_seed(numpy_seed, jit_nullary("np.random.rand"))
 
+    def _check_random_sized(self, seedfunc, randomfunc):
+        # Our seed() mimicks Numpy's.
+        r = np.random.RandomState()
+        for i in [0, 1, 125, 2**32 - 1]:
+            # Need to cast to a C-sized int (for Numpy <= 1.7)
+            r.seed(np.uint32(i))
+            seedfunc(i)
+            for n in range(10):
+                self.assertPreciseEqual(randomfunc(n), r.uniform(0.0, 1.0, n))
+
+    def test_numpy_random_sized(self):
+        self._check_random_sized(numpy_seed, jit_unary("np.random.random_sample"))
+        self._check_random_sized(numpy_seed, jit_unary("np.random.ranf"))
+        self._check_random_sized(numpy_seed, jit_unary("np.random.sample"))
+        self._check_random_sized(numpy_seed, jit_unary("np.random.rand"))
+
     def test_independent_generators(self):
         # PRNGs for Numpy and Python are independent.
         N = 10