python day 8 random library and Monte Carlo methods

Random libraries

I. Overview

1. Random libraries are using random numbers python standard library

2. Generating a pseudo-random number actually. Generated using Mersenne Twister.

3. Two types of function, commonly used 8 Ge

(1) The basic random function: SEED (), Random ()

(2) the extended random function: the randint (), getrandbits (), Uniform (), randrange (), Choice (), shuffle () shuffling

Second, the basic random function

1.Seed()

(1) random number seed assigned to the variable corresponding to the rotation of Mason, Mason generates a rotation sequence. The default is the current system time.

(2) if the same random number seed, it will be a specific order to generate the same random number, i.e., reproduction can be achieved before the implementation of the code. Because every time a random number is the same number of columns.

2.Random()

(1) generates a 0 to 1 decimal.

(2) can not be reproduced, even if the same code, the same is no longer possible since the parameter assignment, the program is difficult to reproduce the original random number is the number used in the end.

(3)

Third, the expansion random function

1. Generate integer

(1) Randint (a, b ) generates a random A , B integer

(2) Randrange (m, n [, k]) generates a random m , n- between to k in steps of integer

(3) Getrandbits (k) to generate a k random bits long integer

2. Generate a decimal

(1) Uniform (a, b ) generating A , B random decimal between

3. sequence

(1) Choice (seq) from the sequence seq select a random element

(2) Shuffle (seq) sequence seq elements in random order

IV Examples - Calculation of pi -

1. Monte Carlo method

2.import random as r

3.import time as t

4.start=t.perf_counter()

5.square=5000*5000

6.hits=0

7.for i in range(square):

8.    a,b=r.random(),r.random()

9.    if pow(pow(a,2)+pow(b,2),0.5)<=1:

10.        hits+=1

11. print ( ' Pi is: {}'. the format (*. 4 Hits / Square))

12. print ( ' run time is: {:} .7f'. the format (t.perf_counter () - Start))

Fifth, mathematical thinking and computational thinking

1. Mathematical Thinking: find the formula to solve with the formula, this time the computer is senior point calculators, does not require any programming

2. Computational thinking: abstract, computer automation to solve.

3. Who is more accurate? Hard to say.

4. Four Color Theorem (one of the three math problems the modern world), also known as four-color conjecture, four-color problem is one of the world's three major mathematical conjecture. Essence four color theorem is an intrinsic property of the two-dimensional plane, i.e. not occur plane intersecting straight lines without common point. Many configurations not proved five or more two-dimensional plane twenty-two connected region, but it did not rise to the level of logic and a two-dimensional intrinsic properties, so there have been many cases of the pseudo inverse. But these are precisely the rigor of research and promote the development of graph theory. Although it has been proved that the computer tens of billions of judgment, but that is only successful on a large number of advantages, which does not meet the strict mathematical logic system, which are still numerous studies mathematics enthusiasts to join.

Sixth, program performance and uptime

1. Most of the time is spent running on the loop, the loop structure concerns

2. Use time library acquisition program run time

3. The program segments, with time performance library time, the test program

 

 

 

 

Guess you like

Origin www.cnblogs.com/cfqlovem-521/p/11886429.html