The method of generating random numbers in Python is mainly concentrated in the random sub-module of the NumPy module:
Import NumPy as NP
(1) generates a random floating-point number with a range of 0-1:np.random.random ()
(2) generate a random floating-point number within a specified range: Np.random.uniform (A, B)
Here, yes. A A, a, a parameter refers to a range
(3) Generate random integers in the specified range: Np.random.randint (A, B)
(4) Generate any number in the specified range: Print Randrange (A, B)
Generates any n incrementing sequence in a specified range: Print Randrange (a,b,n)
(5) Random acquisition of an element
Random.choice (Sequence)
random.choice("Hello world!")#输出随机字符 random.choice(["Hello","world"])#输出随机的单词
(6) Generate a random array:
Rand (D0,D1...,DN): Generates n-dimensional arrays, parameters are the number of each dimension, the elements are [0,1] floating-point numbers, subject to uniform distribution
Randint (Low,hight, (Shape)): The preceding parameter represents the range, and the following parameter represents the shape
Randn (D0,D1...DN): Similar to the first one, but this obeys the normal distribution
(6) There are two common ways to disrupt elements
List=[' A ', ' B ', ' C '] random.shuffle (list)
(7) Randomly select n elements from a sequence without altering the original sequence
A= "123456" b=[1,2,3,4,5,6] c=[' A ', ' B ', ' C ', ' d ', ' e ']
Np.random.sample (a,3)
Np.random.sample (b,3)
Np.random.sample (c,3)
This article is written in reference to other articles. Ha
Summary of Python random number generation method