In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the use of Python's Math package and random package". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the function of Python's Math package and random package?"
Math package
The math package mainly deals with math-related operations. The math package defines two constants:
Math.e # Natural constant e
Math.pi # pi pi
In addition, the math package has a variety of operation functions (the functions of the following functions can be found in the math manual):
Math.ceil (x) # rounds x up, such as xx 1.2, and returns 2
Math.floor (x) # rounds x down, such as xcircle 1.2, and returns 1
Math.pow (XBI y) # exponent operation to get the y power of x
Math.log (x) # logarithm, default base is e You can use the base parameter to change the logarithmic base. For example, math.log (100 Baseball 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 take an x in radians (radian) as an argument.
Angle and Radian interchange: math.degrees (x), math.radians (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)
Random package
If you already know the principle of pseudo-random number (psudo-random number), you can use the following:
Random.seed (x)
To change the seed seed of the random number generator. If you don't know how it works, you don't have to set seed,Python to help you choose seed.
1) Random selection and sorting
Random.choice (seq) # randomly picks an element from the sequence, such as random.choice (range (10)), and randomly picks an integer from 0 to 9.
Random.sample (seq,k) # randomly selects k elements from the sequence
Random.shuffle (seq) # randomly sorts all elements of the sequence
2) randomly generate real numbers
The real numbers generated below are uniformly distributed (uniform distribution), meaning that each number in a range has an equal probability of occurrence:
Random.random () # randomly generates the next real number, which is in the range of [0prime1).
Random.uniform (a _ force b) # randomly generates the next real number, which is in the range of [a _ r _ b].
The real numbers generated below are consistent with other distributions (you can refer to some statistical books to learn about these distributions):
Random.gauss (mu,sigma) # randomly generates random numbers that conform to the Gaussian distribution, and mu,sigma is two parameters of the Gaussian distribution.
Random.expovariate (lambd) # randomly generates random numbers that conform to the exponential distribution, and lambd is the parameter of the exponential distribution.
In addition, there are logarithmic distribution, normal distribution, Pareto distribution, Weibull distribution, please refer to the following link:
Http://docs.python.org/library/random.html
Suppose we have a group of people participating in the dance competition, and for the sake of fairness, we have to arrange their appearance order at random. Let's use the random package to implement:
Import randomall_people = ['Tom',' Vivian', 'Paul',' Liya', 'Manu',' Daniel', 'Shawn'] random.shuffle (all_people) for iMagazine name in enumerate (all_people): print
Practice
Design the following two number generators:
1. 5 integers are randomly selected from 1 to 22 (these 5 numbers are not duplicated)
two。 Randomly generate an 8-digit number, each of which can be any integer from 1 to 6.
At this point, I believe you have a deeper understanding of "what is the role of Python's Math package and random package". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.