# Python Learning note 17: Mathematical correlation of the standard library (math package, random package)

Source: Internet
Author: User

The first few sections look really tired. Now let's start with something simple and easy to understand.

A math package math package mainly deals with mathematical-related operations.

Constant MATH.E # Natural constant E
Math.PI # pi Pi

The arithmetic function Math.ceil (x) # is rounded up to X. x=1.2, for example, returns 2
Math.floor (x) # rounding x down. x=1.2, for example, returns 1
Math.pow (x, y) # exponential operation. Get x's y-square
Math.log (x) # logarithm. The default base is E. Ability to use base parameters. To change the base of the logarithm. For example Math.log (100,base=10)
MATH.SQRT (x) # square root

Trigonometric function

Math.sin (x), Math.Cos (x), Math.tan (x), Math.asin (x), Math.acos (x), Math.atan (x)

Hyperbolic functions

Math.sinh (x), Math.cosh (x), Math.tanh (x), Math.asinh (x), Math.acosh (x), Math.atanh (x)

Special functions

Math.erf (x), Math.gamma (x)

The two random packets assume that you already understand the principle of pseudo-random numbers (psudo-random number). Then you can use for example the following:
Random.seed (x)
To change the seeds seed of the random number generator. Assuming you don't understand the principle, you don't have to specifically set up seed. Python will help you choose Seed.

Randomly pick and sort random.choice (seq) # Randomly selects an element from the elements of the sequence, such as Random.choice (range (10)). Randomly pick an integer from 0 to 9.
Random.sample (seq,k) # random selection of k elements from a sequence
Random.shuffle (seq) # Randomly sort all elements of a sequence

The real numbers generated by randomly generated real numbers conform to the uniform distribution (uniform distribution). means that every number in a range has the same probability of occurring:
Random.random () # randomly generates the next real number, which is within the range of [0,1].
Random.uniform (A, B) # randomly generates the next real number. It is within the range of [A, b].

The following generated real numbers conform to other distributions (you can refer to some statistical books to understand these distributions):
Random.gauss (MU,SIGMA) # randomly generates random numbers that conform to the Gaussian distribution. The Mu,sigma is a Gaussian distribution of two parameters.

Random.expovariate (LAMBD) # randomly generates random numbers that conform to the exponential distribution. LAMBD is an exponential distribution of the number of parameters.

There are also logarithmic distributions, which are normally distributed. Pareto distribution. Weibull distribution

If we have a group of people participating in dance competitions. For the sake of fairness. We have to randomly arrange the order of their appearances. We implement the following using the random package:

`Import randomall_people = [' Tom ', ' Vivian ', ' Paul ', ' Liya ', ' Manu ', ' Daniel ', ' Shawn ']random.shuffle (all_people) for I, Name in Enumerate (all_people):    print (i, ': ' +name)`

Python Learning note 17: Mathematical correlation of the standard library (math package, random package)

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.