Introduction to the random number of the Python standard library (math package, random package) _python

Source: Internet
Author: User
Tags shuffle sin square root in python

We've seen Python's most basic mathematical operations in Python operations. In addition, the math package complements more functions. Of course, if you want more advanced math, consider selecting NumPy and scipy projects outside of the standard library, which not only support array and matrix operations, but also rich mathematical and physical equations to use.

In addition, the random package can be used to generate random numbers. Random numbers can be used not only in mathematical applications, but also in algorithms, to improve the efficiency of the algorithm and improve the security of the program.

Math Package

The math package deals primarily with mathematical-related operations. The math package defines two constants:

Copy Code code as follows:

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

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

Copy Code code as follows:

Math.ceil (x) # takes an integer up, such as x=1.2, to return 2
Math.floor (x) # takes an integer down, such as x=1.2, to return 1
Math.pow (x,y) # exponential operation, get x y-square
Math.log (x) # logarithm, the default base is E. You can use the base parameter to change the bases of the logarithm. Like 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 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 Bag

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

Copy Code code as follows:

Random.seed (x)

To change the seeds seed of the random number generator. If you don't understand its rationale, you don't have to specifically set Seed,python to help you choose Seed.

1) random selection and sorting

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

2) random generation of real numbers

The following generated real numbers conform to the uniform distribution (uniform distribution), meaning that each number within a range has the same probability of appearing:

Copy Code code as follows:

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 in the [A,b] range.

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

Copy Code code as follows:

Random.gauss (MU,SIGMA) # randomly generates random numbers that conform to the Gaussian distribution, and Mu,sigma is the 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.

In addition, there are logarithmic distribution, normal distribution, Pareto distribution, Weibull distribution, can refer to the following links:

Http://docs.python.org/library/random.html

Let's say we have a group of people in the dance competition, and for the sake of fairness, we have to randomly arrange the order of their appearances. We use the random package below to implement:

Copy Code code as follows:

Import Random
All_people = [' Tom ', ' Vivian ', ' Paul ', ' Liya ', ' Manu ', ' Daniel ', ' Shawn ']
Random.shuffle (All_people)
For I,name in Enumerate (all_people):
Print (i, ': ' +name)

Practice

Design the following two types of lottery numbers generators:

1. Randomly extract 5 integers from 1 to 22 (these 5 numbers do not repeat)

2. Randomly generate a 8-digit number, each digit can be 1 to 6 of any number of integers.

Summarize

Math.floor (), Math.sqrt (), Math.sin (), Math.degrees ()

Random.random (), Random.choice (), Random.shuffle ()

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.