Somnath Rakshit
Somnath Rakshit

Reputation: 625

Numpy generate random array based on another array's values

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

Upvotes: 3

Views: 498

Answers (1)

Paul Panzer
Paul Panzer

Reputation: 53119

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

Upvotes: 3

Related Questions