Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy generate random array based on another array's values

Tags:

random

numpy

I have 2 numpy arrays, a and b. Here:

a = np.random.randint(501, size=100)

How can I randomly generate array b of size 100 in a vectorized way such that:

  1. b[i] > a[i] for all values of i.
  2. Each value of b lies between [0, 501).
like image 470
Somnath Rakshit Avatar asked Jul 19 '26 08:07

Somnath Rakshit


1 Answers

numpy supports array parameters. You can use a as the lower boundary:

>>> rng = np.random.default_rng()
>>> 
>>> a = rng.integers(501,size=10)
>>> 
>>> a
array([ 82,  95, 463, 367, 257, 296, 449, 473, 202, 468])
>>> 
>>> b = rng.integers(a,501)
>>> 
>>> b
array([104, 153, 476, 376, 366, 391, 458, 474, 470, 499])
like image 108
Paul Panzer Avatar answered Jul 21 '26 11:07

Paul Panzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!