Random and math modules in Python learn notes

Source: Internet
Author: User
Tags constant cos mathematical constants pow range shuffle sin square root


This article mainly introduces Python random and math module learning notes, this article explains the math module mathematical constants, commonly used simple functions, trigonometric functions, etc., explained the random module of the common functions, random selection and sorting, etc., the need for friends can refer to the



Due to the recent frequent use of Random,math and Time ' datetime ' in Python, I decided to spend a systematic study



1. Math Module



Functions in math can not be used for too complex a number of operations, if you need to run a complex number of functions with the same name in the Cmath module, if you want more advanced mathematical functions, you can consider the selection of NumPy and scipy modules outside the standard library, they support not only array and matrix operations, There are plenty of mathematical and physical equations to use.



1.1. Mathematical Constants



Math.PI this mathematical constant equals 3.141592 ...



MATH.E this mathematical constant e = 2.718281 ...,



1.2. Common simple functions



Math.ceil (x): Rounding up the X, returning the smallest integer value greater than or equal to X



The code is as follows:



#-*-Coding:utf-8-*-



Import Math #仅在第一次声明, the following will be omitted



Print Math.ceil (Math.PI) #math. Pi is pi Pi, similar to macros in C/s + +



Output 4



Math.floor (x): Rounding down to X, returning an integer value less than or equal to X



The code is as follows:



>>> Import Math



>>> Math.floor (Math.PI)



3.0



Math.pow (x,y): exponential operation, get x y-square



The code is as follows:



>>> Math.pow (2, 3)



8.0



Math.log (x[, Base]): Logarithmic operation, the default base is the logarithmic operation of E. When using the base parameter, change the base of the logarithm into a base-based logarithmic operation



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) Take absolute value



Math.factorial (x) to find factorial, i.e. x!



Math.exp (x) to find the X-square of E



1.3. Trigonometric Functions



The following functions receive an X in radians (radian) as an argument



The code is as follows:



Math.acos (x) #arccos (x)



Math.asin (x) #arcsin (x)



Math.atan (x) #arctan (x)



Math.Cos (x) #cos (x)



Math.sin (x) #sin (x)



Math.tan (x) #tan (x)



Math.degrees (x) angle system into radians



Math.radians (x) radian conversion to angle system



The code is as follows:



>>> Math.degrees (MATH.PI/2)



90.0



1.5. Hyperbolic functions and special functions



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



Some of the functions are basically useless.



2. Random Module



The function of the random module is to generate the random number, which realizes the pseudo random number generator



1.1. Common functions



Random.seed ([x]) User Initializes a random number seed, optional parameter can be any Hashtable object, default to use system time



Random.randint (A, B) returns an integer between A and B



Random.randrange ([start], stop[, step] gets a random number from the specified range in a collection that is incremented by the specified cardinality. such as: Random.randrange (10, 100, 2), the result is equivalent from [10, 12, 14, 16, ... 96, 98] sequence to obtain a random number. Random.randrange (10, 100, 2) are equivalent to Random.choice (range (10, 100, 2) on the results.



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 sequencing



Random.choice (Sequence): Gets a random element from the sequence. The parameter sequence represents an ordered type. Here's how: sequence is not a specific type in Python, it's a series of types. list, tuple, strings 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 obtains a fragment of the specified length 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]), which is used to disrupt 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. Random generation of real numbers



The generated real numbers conform to uniform distributions (uniform distribution)



Random.random () randomly generates the next real number, which is in the range of [0,1].



Random.uniform (a,b) randomly generates the next real number, which is within the [A,b] range.



The code is as follows:



>>> Random.random ()



0.019433835195078797



>>> Random.uniform (3, 8)



6.830376841208885



Random.gauss (mu,sigma) randomly generates random numbers that conform to the Gaussian distribution, and the Mu,sigma is two parameters of the Gaussian distribution.



Random.expovariate (LAMBD) randomly generates random numbers that conform to exponential distribution, and LAMBD is the parameter of exponential distribution.


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.