uniQ
uniQ

Reputation: 125

Numpy Random 2D Array

I want to create a 2D uniformly random array in numpy which is something like:

A=[[a1,b1],
   [a2,b2],
   ...,
   [a99,b99]]

But I want the values of the A column between a certain range (say between 1-10) and values of B within a different range (say 11-20).

How would this be obtained in Python?

Upvotes: 5

Views: 8542

Answers (1)

Divakar
Divakar

Reputation: 221754

Two ways.

We could stack two random arrays with properly assigned low and high values -

In [39]: n = 10000 # no. of rows

In [40]: np.c_[np.random.randint(1,11,(n)), np.random.randint(11,21,(n))]
Out[40]: 
array([[ 6, 19],
       [ 8, 18],
       [ 6, 11],
       ..., 
       [ 5, 12],
       [10, 16],
       [ 7, 17]])

In [41]: _.min(0), _.max(0) # verify
Out[41]: (array([ 1, 11]), array([10, 20]))

Another would be to create 2D random array with [1,10] interval and then add 10 offset for the second column, thus getting us [11,20] interval for it -

In [42]: np.random.randint(1,11,(n,2)) + [0,10]
Out[42]: 
array([[10, 16],
       [ 9, 12],
       [ 4, 17],
       ..., 
       [ 7, 15],
       [ 5, 11],
       [ 4, 14]])

In [43]: _.min(0), _.max(0) # verify
Out[43]: (array([ 1, 11]), array([10, 20]))

Upvotes: 4

Related Questions