import random
count = 0
times = 100000
for i in range(times):
x = random.uniform(-1 , 1)
y = random.uniform(-1 , 1)
if x*x + y*y <= 1:
count += 1
s = count * 4 / times
print(s)
几次运行结果
import random
count = 0
times = 100000
for i in range(times):
x = random.uniform(-1 , 1)
y = random.uniform(-1 , 1)
if x*x + y*y <= 1:
count += 1
s = count * 4 / times
print(s)
几次运行结果