3

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

1 Answer 1

3

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])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.