Python commonly used numpy and random random number generation

Insert picture description here

1. Use of Python's built-in library random

import random
  • Generate 1 int random number in the range of n~m :random.randint(n,m)
random.randint(1,5)

Insert picture description here

  • Generate a float random number between n and m :random.uniform(n, m)
random.uniform(n, m)

Insert picture description here

  • Generate a float random number between 0 and 1 :random.random()
random.random()

Insert picture description here

  • Generate an int integer with an interval of k from n to m :random.randrange(n,m,k)
random.randrange(n,m,k)

Insert picture description here

  • Randomly select 1 element from the sequence: random.choice(list)
random.choice([1, 2, 3.4, 4.2, 5.6, 6])

Insert picture description here

  • List out-of-order operation: random.shuffle(list);Note: This function has no return value, and the original list is directly modified
a = [1,3,5,6,7]
# 或 a = np.array([1,3,5,6,7])
random.shuffle(a)

Insert picture description here

Two, Numpy generates an array of random numbers

import numpy as np

[0~1 uniformly distributed float vector or array]: Generate n random numbers between 0-1: np.random.random(n)

np.random.random(n)

Insert picture description here
There is another way to have the same function: np.random.rand(d1,d2,d3,...,dn)

np.random.rand(2,3,5)

For example, to generate a 2×3×5uniformly distributed random number array between 0 and 1 of dimension, the algorithm of the following
Insert picture description here
randomand randis exactly the same, except that the method of passing parameters is different. The reason for setting rand may be related to matlab due to historical reasons. Please refer to stackoverflow for details .

[ Nm uniformly distributed int vector or array]: Generate an int type random number array between n~m :np.random.randint(n,m,size=d)

np.random.randint(n,m,size=d)
np.random.randint(n,m,size=(d1,d2,...))

Insert picture description here

N ( 0 , 1 ) N(0, 1) N(0,1 ) Normally distributed float array]: Generate N-dimensionalrandom numbers:服从 N ( 0 , 1 ) 的 N(0, 1)的 N(0,1)正态分布np.random.randn(d1,d2,...,dn)

np.random.randn(2,3,4)

For example, to produce a 2×3×4dimension of N(0, 1) that obeys N (0, 1)N(0,1 ) The normally distributed random number array is as follows, we can see that there are only a few[-1,1]random numbers outside:

Insert picture description here

【Random Draw】:np.random.choice(list_or_array, size=None, replace=True, p=None)

The function of this choice is more powerful than the built-in choice function of python. You can customize the probability of each element being extracted and whether it is replaced or extracted.

  • size : the size of the array or list, 1-dimensional filled with integers, multi-dimensional filled(d1,d2,....)
  • replace : Whether there is replacement extraction, True means yes, it may extract repeated values ​​multiple times, False will not extract repeated values
  • p : The probability prob that each element of the list or array is extracted, fill in p=[p1,p2,...], and ensure that the total probability=1
numpy.random.choice(a, size=None, replace=True, p=None)

Insert picture description here

[References]:
[1] https://blog.csdn.net/zq476668643/article/details/95219453 .
[2] Stackoverflow answers.

Guess you like

Origin blog.csdn.net/SL_World/article/details/108766215