Study notes of the Random and Math modules in Python

Source: Internet
Author: User
Tags mathematical constants mathematical functions
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 the common functions, random selection, and sorting of the random module. For more information, see the random, math, and time''datetime modules in Python, so I decided to spend some time studying the 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.

The rest are some functions that have not been used currently, and will be used later.

3. Reference links

Random official documentation
Official math documentation

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.