Can I generate an array with both negative and positive numbers and specify the limits and the dimension of this array?

后端 未结 1 1798
深忆病人
深忆病人 2021-01-15 02:49

c=np.random.rand(10,2) generates an array of random numbers from in [0,1). Can I generate the same array (in terms of size) but with negative AND p

相关标签:
1条回答
  • 2021-01-15 03:26

    Use np.random.randint and pass the size parameter. For values in [-2, 2) and size (10, 2) you have:

    In [32]: np.random.randint(-2, 2, (10, 2))
    Out[32]:
    array([[-1,  1],
           [-2,  0],
           [-1,  0],
           [-2, -2],
           [ 0, -2],
           [-2, -2],
           [ 0,  0],
           [ 1, -1],
           [ 0,  1],
           [-2, -1]])
    

    Or use np.random.uniform for floats:

    In [33]: size = (10, 2)
    
    In [39]: np.random.uniform(-2, 2, size)
    Out[39]:
    array([[-1.1129566 , -1.94562947],
           [ 0.21356557, -1.96769933],
           [ 1.1481992 , -0.08494563],
           [ 0.12561175,  0.95580417],
           [-1.79335536, -1.01276994],
           [ 1.56808971,  0.15911181],
           [ 0.50987451, -1.39039728],
           [-1.57962641,  1.59555514],
           [-1.8318709 , -1.57055885],
           [ 0.682527  , -1.89022731]])
    
    0 讨论(0)
提交回复
热议问题