jeudi 23 septembre 2021

how to use fastrand in a numpy array

I'd like to use fastrand library provided on this GitHub page, which works fine when used to generate random numbers one at a time. For instance,

#pip install fastrand==1.2.4
import fastrand

print(fastrand.pcg32bounded(1001))
975

However, I would like to use this method in a numpy array to generate multiple numbers for certain indices where the upper bound is the number existing in the corresponding index. Suppose I have a 2 by 5 numpy array.

import fastrand
arr = np.array([[np.random.randint(2,10) for i in range(5)]for j in range(2)])
print(arr,"\n")

[[8 7 4 9 9]
 [9 9 9 7 7]] 

Now, I substract random numbers for the first two numbers in the second row. I can easily do this by using np.random.randint as shown below.

arr[1,:2]-=  np.random.randint(arr[1,:2]) 
print(arr)
[[8 7 4 9 9]
 [4 1 9 7 7]]

My goal is to use fastrand instead of np.random.randint to increase the performance. Is this possible by any chance?




Aucun commentaire:

Enregistrer un commentaire