# How to use np.random.default\_rng with numba jit

**URL:** <https://numba.discourse.group/t/how-to-use-np-random-default-rng-with-numba-jit/1295>\
**Category:** Community Support\
**Created:** [April 12, 2022, 1:48pm UTC](https://numba.discourse.group/t/how-to-use-np-random-default-rng-with-numba-jit/1295 "2022-04-12T13:48:29Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ali\_Sh](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Ali\_Sh](https://numba.discourse.group/u/Ali_Sh)\
**Post date:** [April 12, 2022, 1:48pm UTC](https://numba.discourse.group/t/how-to-use-np-random-default-rng-with-numba-jit/1295/1 "2022-04-12T13:48:29Z")

</div>

I would jitting a function in which `np.random.default_rng` is used as `rng`. I don’t know how to use the related code line in this case; Is it better to use it inside the function or out of it, how to control it in the both conditions?  
I have read about it in the numba doc which said that we can do it with something like `with objmode(y="float64[:]"`, but I could not use it correctly in my code. How could I run this function by `nb.jit()` or `nb.njit()`?

```auto
import numpy as np
import numba as nb

rng = np.random.default_rng(85)
min_lim = np.load("min_lim.npy")
max_lim = np.load("max_lim.npy")
NSG = np.load("NSG.npy")

# @nb.jit("float64[:, ::1], float64[:, ::1], int32[:]")
def rand_pos(min_lim, max_lim, NSG):
    # Rand_Pos = np.zeros(1, NSG.shape[0]) # for parallelization if it could
    # with objmode(rand_x="float64[:]", rand_y="float64[:]", rand_z="float64[:]"):
    # rng = np.random.default_rng(85)
    Rand_Pos_ = []
    for i, j in enumerate(NSG):
        mi_0, mi_1, mi_2 = min_lim[i]
        ma_0, ma_1, ma_2 = max_lim[i]
        rand_pos_cell = []
        for m in range(j):
            rand_x = rng.uniform(mi_0, ma_0, 1)
            rand_y = rng.uniform(mi_1, ma_1, 1)
            rand_z = rng.uniform(mi_2, ma_2, 1)
            rand_pos_cell.append([rand_x, rand_y, rand_z])

        Rand_Pos_.append(rand_pos_cell)

    return Rand_Pos_

new_ = rand_pos(min_lim, max_lim, NSG)

```

I would be grateful for any recommendations about how to improve the performance on this code if possible (e.g. parallelization, …). Does list appending have conflict with parallelization if it could. It must be said that I don’t know if I am asking in the right place due to my new familiarity with numba.

data: `https://drive.google.com/file/d/15igYymSlf7YCAT_9Gm9_w0Dz5EeBxNf-/view?usp=sharing`
