current position:Home>Python---- Monte Carlo method to find Π, stopwatch function

Python---- Monte Carlo method to find Π, stopwatch function

2022-11-08 06:41:42will be open today

Monte Carlo Π:

Sector area/square area=points in sector/points in square

time.process_time(): Press the stopwatch function

time.process_time(): Lift the stopwatch function

#Import time, random libraryimport randomimport time#square area is 1, sector area=pi*1/4=n/m ==》 pi=4*n/m# Sector area/unit square area = number of points in sector area/total number of points in unit squaren=eval(input())#Enter the total number of pointsm=0time.process_time()#Press the stopwatch function.for i in range(n):x=random.random()y=random.random()if (x**2+y**2)**(1/2)<=1: #Determine whether the generated point is within the sectorm+=1pi=4*m/nprint(time.process_time())#lift stopwatch 0,print(pi)

copyright notice
author[will be open today],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/312/202211080628358322.html

Random recommended