A detailed description of the mathematical and random numbers in the Python standard library (math package, random package)

Source: Internet
Author: User
Tags mathematical functions shuffle
we are already inPython The most basic mathematical functions of python are seen in the operation. In addition, Math Package added a lot ofFunction . Of course, if you want more advanced math features, consider choosing a standard library NumPy and the scipy projects, they not only supportArray and matrix operations, as well as a wealth of mathematical and physical equations to use.

In addition, the randompackage can be used to generate stochastic numbers . The random number can not only be used for mathematical purposes, but also often embedded in the algorithm to improve the efficiency of the algorithm and improve the security of the program.

Math Package

The math package mainly handles mathematical-related operations. The math package defines two constants:

MATH.E   # Natural constant Emath.pi  # pi Pi

In addition, the math package also has a variety of arithmetic functions (the function of the following functions can refer to the math manual):

Math.ceil (x)       # Rounding up the x, such as x=1.2, returns 2math.floor (x)      # to the x down rounding, such as x=1.2, returns the 1math.pow (x, y)      # exponential operation, The Y-square math.log (x) # logarithm of x is obtained        , and the default base is E. You can use the base parameter to change the base of the logarithm. such as Math.log (100,base=10) math.sqrt (x)       # square-Root trigonometric functions: Math.sin (x), Math.Cos (x), Math.tan (x), Math.asin (x), Math.acos (x) , Math.atan (x)

These functions receive an X in radians (radian) as a parameter.

Angle and Radian Interchange: Math.degrees (x), Math.radians (x) hyperbolic function: Math.sinh (x), Math.cosh (x), Math.tanh (x), Math.asinh (x), Math.acosh (x) , Math.atanh (x) Special function: Math.erf (x), Math.gamma (x)

Random Package

If you already know the principle of pseudo-random numbers (psudo-random number), then you can use the following:

Random.seed (x)

To change the seeds seed of the random number generator. If you don't know how it works, you don't have to specifically set Seed,python to help you choose Seed.

1) random selection and sorting

Random.choice (seq)   # Randomly picks an element from a sequence of elements, such as Random.choice (range (10)), and randomly picks an integer from 0 to 9. Random.sample (seq,k) # randomly selects K elements random.shuffle (seq) from a sequence  # randomly sorts all elements of a sequence

2) randomly generated real numbers

The resulting real numbers conform to the uniform distribution (uniform distribution), which means that each number in a range appears equal in probability:

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, which is within the range of [A, b].

The resulting real numbers correspond to other distributions (you can refer to some statistical books to understand these distributions):

Random.gauss (Mu,sigma)    # randomly generates a random number that conforms to the Gaussian distribution, and the Mu,sigma is two parameters of the Gaussian distribution. Random.expovariate (LAMBD) # randomly generates a random number that conforms to the exponential distribution, LAMBD is the parameter of the exponential distribution.

There are also logarithmic distributions, normal distribution, Pareto distributions, and Weibull distributions, which can be found in the following links:

Docs.python.org/library/random.html

Suppose we have a group of people in a dance competition, in order to be fair, we have to randomly arrange their appearances. Below we use the random package to implement:

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

"Recommended"

1. Share a tutorial on how to generate random numbers in Python module

2. Example tutorial for Python random () function

3. Share an example tutorial on random (randomly generated numbers) in Python

4. Share Python Random example of a method that generates n random numbers that are not duplicated in a range

5. Python Random Module (get random number) common methods and use examples

6. Python Random Module Common methods

7. Python Module Learning: Random number generation

Related Article

Contact Us

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.