Introduction to the random number of math package and random package of Python standard library

  • 2020-04-02 14:23:19
  • OfStack

We have seen Python's most basic mathematical functions in Python operations. In addition, the math package adds more functions. Of course, if you want more advanced math capabilities, consider the numpy and scipy projects outside the standard library, which support not only array and matrix operations, but also a wealth of mathematical and physical equations.

In addition, the random package can be used to generate random Numbers. Random Numbers are not only used for mathematical purposes, but are often embedded in algorithms to improve algorithm efficiency and program security.

Math package

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


math.e   # constants e
math.pi  # PI pi

In addition, the math package has various arithmetic functions (see the math manual for the following functions) :


math.ceil(x)       # right x I rounded it up, for example x=1.2 To return to 2
math.floor(x)      # right x I rounded it down, for example x=1.2 To return to 1
math.pow(x,y)      # Exponential operation, get x the y To the power
math.log(x)        # Logarithm, the default basis is e . You can use base Parameter to change the base of the logarithm. Such as math.log(100,base=10)
math.sqrt(x)       # The square root

Math.sin (x), math.cos(x), math.tan(x), math.asin(x), math.acos(x), math.atan(x)

Each of these functions takes an x in radian as an argument.

Swap degrees and radians: 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)

The random packet

If you already know the principle of psudo-random number, you can use the following:


random.seed(x)

To change the seed of the random number generator. If you don't know how it works, you don't have to specifically set the seed; Python selects the seed for you.

1) random selection and sorting

The random choice (seq)     Pick a random element from the elements of the sequence, such as random. Choice (range(10)), and pick a random integer from 0 to 9.
Random. Sample (seq,k) # randomly select k elements from the sequence
The random shuffle (seq)   Sort all the elements of the sequence at random

2) random generation of real Numbers

The real Numbers generated below conform to a uniform distribution, meaning that each number in a range has an equal probability:


random.random()          # I'm going to randomly generate the next real number [0,1) Within the scope.
random.uniform(a,b)      # I'm going to randomly generate the next real number [a,b] Within the scope.

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


random.gauss(mu,sigma)    # Randomly generate random Numbers in accordance with the gaussian distribution, mu,sigma Is two parameters of the gaussian distribution.
random.expovariate(lambd) # Random generation of random Numbers consistent with exponential distribution, lambd Is the parameter of exponential distribution.

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

(link: http://docs.python.org/library/random.html)

Let's say we have a group of people participating in a dance competition, and to be fair, we're going to randomly rank them. Next, we use the random package to implement:


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 lottery number generators:

1. Randomly select 5 integers from 1 to 22 (these 5 Numbers do not repeat)

2. Randomly generate an 8-digit number, each of which can be any integer from 1 to 6.

conclusion

Math. The floor (), math. SQRT (), math. The sin (), math. Degrees ()

Random. The random (), the random choice (), random. Shuffle ()


Related articles: