5

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?

2
  • What's the shape of output array? Commented Oct 27, 2017 at 23:19
  • The array will contain 500 rows and 2 columns. (column 1 will be in range 1-10; and column 2 will be in range 11-20) Commented Oct 27, 2017 at 23:34

1 Answer 1

4

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