numpy.random

function Functional description The return value
Np. Random. Rand (d0, d1,... ,dn) An array whose elements are evenly distributed between [0,1) Floating point Numbers
np.random.uniform(low,hige, size) An array whose elements are evenly distributed between [Low, Hige] Floating point Numbers
numpy.random.randint(low,hige, size) An array whose elements are evenly distributed between [Low, Hige] The integer
Np. Random. Randn (d0, d1,... ,dn) Produces a standard normally distributed array Floating point Numbers
np.random.normal(loc, scale, size) Produces a normally distributed array Floating point Numbers
# create 2*3 random numbers, each number is a floating point number between [0,1)
>>> np.random.rand(2.3)
array([[0.63781021.0.56879887.0.01676   ],
       [0.69158309.0.80349676.0.7012152 ]])

# return a number if the argument is empty
>>> np.random.rand()
0.2780463068554154

Create a random set of 3*2 numbers, each of which is a floating point number between [1,5]
>>> np.random.uniform(1.5, (3.2))
array([[2.56321923.3.03023465],
       [1.85914173.2.16025346],
       [4.64388149.4.01960314]])
       
Create a random set of 3*2 numbers, where each number is an integer between [1,5]
>>> np.random.randint(1.5, (3.2))
array([[4.1],
       [3.3],
       [4.4]])

# create a 2*3 random number set that conforms to the standard normal distribution
>>> np.random.randn(2.3)
array([[-0.21078328, -0.46365853.1.17982503],
       [ 0.93104933, -0.14153484.1.48260646]])

# create a random group of 3*2 numbers with normal distribution, mean = 0, variance = 1
>>> np.random.normal(0.1, (3.2))
array([[-1.38009084.0.23037739],
       [ 0.91970242, -0.86786995],
       [ 0.32031551, -0.11701534]])
Copy the code

Set random seed

>>> np.random.seed(123)
>>> np.random.rand(2.3)
array([[0.69646919.0.28613933.0.22685145],
       [0.55131477.0.71946897.0.42310646]])
       
>>> np.random.seed(123)
>>> np.random.rand(2.3)
array([[0.69646919.0.28613933.0.22685145],
       [0.55131477.0.71946897.0.42310646]])
       
>>> np.random.rand(2.3)
array([[0.9807642 , 0.68482974.0.4809319 ],
       [0.39211752.0.34317802.0.72904971]])
Copy the code

Perturb the order function minusshuffle()

Np.random. Shuffle (sequence) is used for one-dimensional arrays

>>> arr = np.arange(10)
>>> arr
array([0.1.2.3.4.5.6.7.8.9])
>>> np.random.shuffle(arr)
>>> arr
array([5.6.2.9.1.8.7.0.4.3])
Copy the code

Use with multidimensional arrays

>>> b = np.arange(12).reshape(3.4)
>>> b
array([[ 0.1.2.3],
       [ 4.5.6.7],
       [ 8.9.10.11]])
>>> np.random.shuffle(b)
>>> b
array([[ 4.5.6.7],
       [ 0.1.2.3],
       [ 8.9.10.11]])
Copy the code