Study Notes of the Random and Math modules in Python
This article mainly introduces the study notes of the Random and Math modules in Python. This article describes the mathematical constants, common simple functions, and trigonometric functions of the math module, describes common functions, random selection, and sorting of the random module. For more information, see
Since the random, math, and time''datetime modules in Python are often used recently, it is decided to take a moment to learn about the time system.
1. math Module
Functions in math cannot be used for operations that are too complex. If you need to run complex numbers, you 'd better use functions of the same name in the cmath module. If you want more advanced mathematical functions, consider the numpy and scipy modules outside the standard library. They not only support array and matrix operations, but also support a wide range of mathematical and physical equations.
1.1. mathematical constants
Math. pi: this mathematical constant is equal to 141592...
Math. e, the mathematical constant e = 2. 718281 ...,
1.2. Common simple functions
Math. ceil (x): returns an integer greater than or equal to x rounded up.
The Code is as follows:
#-*-Coding: UTF-8 -*-
Import math # is declared only for the first time.
Print math. ceil (math. pi) # math. pi is the circumference rate pi, which is similar to the macro in C/C ++.
// Output 4
Math. floor (x): returns an integer less than or equal to x rounded down.
The Code is as follows:
>>> Import math
>>> Math. floor (math. pi)
3.0
Math. pow (x, y): exponential operation to obtain the power y of x
The Code is as follows:
>>> Math. pow (2, 3)
8.0
Math. log (x [, base]): logarithm operation. The default base is the logarithm operation of e. When the base parameter is used, the base of the logarithm is changed to base-on-base logarithm.
The Code is as follows:
>>> Math. log (10)
2.302585092994046
>>> Math. log (8, 2) # log (x)/log (base ).
3.0
Math. sqrt (x) square root Calculation
The Code is as follows:
>>> Math. sqrt (4)
2.0
Math. fabs (x) returns the absolute value.
Math. factorial (x) returns the factorial, that is, x!
Math. exp (x): Calculate the x power of e.
1.3. trigonometric Functions
The following functions receive an x in radians as the parameter.
The Code is as follows:
Math. acos (x) # Calculate arccos (x)
Math. asin (x) # Calculate arcsin (x)
Math. atan (x) # Calculate arctan (x)
Math. cos (x) # Calculate cos (x)
Math. sin (x) # evaluate sin (x)
Math. tan (x) # evaluate tan (x)
Math. degrees (x) angle to radian
Math. radians (x) radians to degrees
The Code is as follows:
>>> Math. degrees (math. pi/2)
90.0
1.5. hyperbolic and Special Functions
Math. sinh (x), math. cosh (x), math. tanh (x), math. asinh (x), math. acossh (x), math. atanh (x)
Some functions have never been used.
2. random Module
The role of the random module is to generate random numbers. This module implements the pseudo-random number generator.
1.1. Common functions
Random. seed ([x]) The user initializes a random number seed. The optional parameter can be any hashtable object. The system time is used by default.
Random. randint (a, B) returns an integer between a and B.
Random. randrange ([start], stop [, step]) obtains a random number from a set in the specified range that increments by the specified base number. For example: random. randrange (10,100, 2), the result is equivalent to [10, 12, 14, 16 ,... 96, 98] obtain a random number from the sequence. Random. randrange (10,100, 2) is equivalent to random. choice (range (10,100, 2) in the result.
Random. randrange (start, stop, step) is equivalent to random. choice (range (start, stop, step ))
The Code is as follows:
>>> Random. randrange (10,100, 2)
90
1.2. Random selection and sorting
Random. choice (sequence): gets a random element from the sequence. The sequence parameter indicates an ordered type. Sequence is not a specific type in python, but a series of types. List, tuple, and string all belong to sequence
The Code is as follows:
>>> Random. choice (range (10 ))
1
>>> Random. choice (1, 2, 3, 4 ))
3
Random. sample (sequence, k) # randomly obtain the fragments with a specified length of k from the specified sequence. The sample function does not modify the original sequence.
The Code is as follows:
>>> Lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> New_lst = random. sample (lst, 6)
>>> Print new_lst
[8, 9, 2, 1, 5, 4]
>>> Print lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Random. shuffle (x [, random]) is used to disrupt the elements in a list without generating a new list.
The Code is as follows:
>>> Lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> Random. shuffle (lst)
>>> Print lst
[10, 5, 2, 7, 3, 9, 4, 8, 6, 1]
1.3. generate real numbers randomly
The generated real number conforms to the uniform distribution (uniform distribution)
Random. random () randomly generates the next real number within the range of [0, 1.
Random. uniform (a, B) randomly generates the next real number, which is within the range of [a, B.
The Code is as follows:
>>> Random. random ()
0.019433835195078797
>>> Random. uniform (3, 8)
6.830376841208885
Random. gauss (mu, sigma) randomly generates random numbers that match the Gaussian distribution. mu and sigma are two parameters of Gaussian distribution.
Random. expovariate (lambd) randomly generates random numbers that match the exponential distribution. lambd is a parameter of exponential distribution.