Python Math library and Random Library

Source: Internet
Author: User
Tags value of pi

1. Math Library

1>>> fromMathImport*2>>>Pi36.2831853071795864>>>e52.7182818284590456>>> Ceil (2.3)738>>> Floor (2.3)92Ten>>> Pow (2,3) One8.0 A>>>log (E) -1.0 ->>> log10 (100) the2.0 ->>> sqrt (16) -4.0

1>>> fromMathImport*2>>> exp (2)37.389056098930654>>> e**257.38905609893064956>>>degrees (PI)7180.08>>> radians (120)92.0943951023931953Ten>>> Sin (pi/2) One1.0 A>>> cos (PI/3) -0.5000000000000001 ->>> Tan (Pi/4) the0.9999999999999999 ->>> Atan (1) -0.7853981633974483 ->>> ASIN (1) +1.5707963267948966 ->>> ACOs (1) +0.0

2. Random Library

1  fromRandomImport*2>>>random ()30.66066489378874784>>> Uniform (1,10)58.3168374234199216>>> Randint (1,10)758>>> Randrange (0,10,2)94Ten>>> Randrange (0,10,2) One8 A>>> Randrange (0,10,2) -6 ->>> Randrange (0,10,2) the 0 ->>> ra=[0,1,2,3,4,5,6,7,8,9] ->>>Choice (RA) -1 +>>>Shuffle (RA) ->>>RA +[0, 2, 3, 9, 7, 8, 5, 6, 1, 4] A>>> Sample (ra,4) at[1, 8, 5, 9]

3. Random seeds

1>>> Seed (10)2>>>random ()30.57140259468991354>>>random ()50.42888905467511466>>>random ()70.57809130113447048>>> seed (1)9>>>random ()Ten0.13436424411240122 One>>>random () A0.8474337369372327 ->>> Seed (10) ->>>random () the0.5714025946899135 ->>>random () -0.4288890546751146 ->>>random () +0.5780913011344704

It can be seen that pseudo-random numbers are generated by random seeds.

4. Monte Carlo (Monte Carlo) method

also known as random sampling or statistics test method. When the problem solved is the probability of an event, or the expected value of a random variable, it can be solved by some kind of "experiment" method.  to put it simply, Monte Carlo is a method of solving problems by using random experiments.

the IPO of the π calculation problem is as follows:

Input: Number of throw points

processing: For each scatter point, calculate the distance from the point to the center, via Distance determines whether the point is within a circle or outside the circle. Count the number of points within a circle

output: π value

1  fromMathImportsqrt2  fromRandomImportRandom3  fromTimeImportClock#Time Library4 5darts=150000#number of throws6Hits=0#Hit Count7 clock ()8  forIinchRange (Darts):9X,y=random (), Random ()#Synchronous AssignmentTen     ifsqrt ((x**2+y**2)) <=1: OneHits=hits+1 Apai=4*hits/Darts - Print("The value of pi is:%f"%Pai) - Print("program run time%s s"%clock ())

Python Math library and Random Library

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.