Since I've been using Python's random, math, and time``datetime modules a lot lately, I decided to take the time to systematically learn how to use them.
1. The math module
math function can not be used for too complex number of operations, if you need to run the complex number of the best use cmath module of the same name function, if you want more advanced mathematical functions, you can consider choosing the standard library outside of the numpy and scipy module, they not only support arrays and matrices, and a wealth of mathematical and physical equations to use
1.1 Mathematical constants
This mathematical constant is equal to 3.141592...
This mathematical constant e = 2.718281... ,
1.2 Common Simple Functions
(x) : rounds up to the nearest integer greater than or equal to x.
# -*- coding:utf-8 -*-
import math #Only the first time it is declared, all the following will be omitted.
print () # is pi, similar to the C/C++ macro
// Output 4
(x) : rounds down to the nearest whole x, returns an integer value less than or equal to x.
>>> import math
>>> ()
3.0
(x,y) : Exponential operation to get the yth power of x.
>>> (2, 3)
8.0
(x[, base]) : Logarithmic operation with base e by default. If you use the base argument, you change the base of the logarithm, and it becomes a logarithmic operation with base as the base.
>>> (10)
2.302585092994046
>>> (8, 2) #log(x)/log(base).
3.0
(x) Square root calculation
>>> (4)
2.0
(x) Taking absolute values
(x) to find the factorial, i.e., x!
(x) Find the xth power of e
1.3. trigonometric functions
The following functions all take an x in radians as an argument
(x) #find arccos(x)
(x) #find arcsin(x)
(x) #find arctan(x)
(x) # Find cos(x)
(x) #find sin(x)
(x) #find tan(x)
(x) Conversion of angular system to radian system
(x) Conversion of radians to angles
>>> ( / 2)
90.0
1.5. Hyperbolic and special functions
(x), (x), (x), (x), (x), (x)
There are also some functions that are largely unused.
2. random module
The role of the random module is to generate random numbers, this module implements a pseudo-random number generator.
1.1 Common functions
([x]) user initialize a random number seed, optional parameters can be any hashtable object, default use system time
(a, b) Returns an integer between a and b.
([start], stop[, step]) Obtains a random number from a set within a specified range, incremented by a specified base. For example, (10, 100, 2) is equivalent to getting a random number from the sequence [10, 12, 14, 16, ... 96, 98]. (10, 100, 2) is equivalent in result to (range(10, 100, 2)).
(start, stop, step) is equivalent to (range(start, stop, step))
>>> (10, 100, 2)
90
1.2 Random selection and sorting
(sequence) : Get a random element from a sequence. The argument sequence represents an ordered type. It should be noted that sequence is not a specific type in python, but a set of types in general. lists, tuples, strings are all sequences.
>>> (range(10))
1
>>> ((1, 2, 3, 4))
3
(sequence, k) # Fetch a random slice of specified length k from the specified sequence. sample function does not modify the original sequence.
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> new_lst = (lst, 6)
>>> print new_lst
[8, 9, 2, 1, 5, 4]
>>> print lst
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(x[, random]), which is used to break elements in a list, without generating a new list.
>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> (lst)
>>> print lst
[10, 5, 2, 7, 3, 9, 4, 8, 6, 1]
1.3 Random generation of real numbers
The generated real numbers conform to a uniform distribution.
() Randomly generates the next real number, which is in the range [0,1).
(a,b) Randomly generate the next real number which is in the range [a,b].
>>> ()
0.019433835195078797
>>> (3, 8)
6.830376841208885
(mu,sigma) Randomly generates random numbers that conform to a Gaussian distribution. mu,sigma are two parameters of the Gaussian distribution.
(lambd) Randomly generates random numbers that conform to an exponential distribution, with lambd being a parameter of the exponential distribution.
The rest are functions that I haven't used so far, but I'll add them later when I need them.
3. Reference links
random official document
Official math documentation