Добавить lec1/mc.py

This commit is contained in:
2026-10-17 08:47:16 +02:00
parent 7c2ef43090
commit 53b31bea26
+12
View File
@@ -0,0 +1,12 @@
import random
def count_hits(n_points, seed):
"""Сколько случайных точек из квадрата [0,1]x[0,1] попало в четверть круга."""
rng = random.Random(seed) # свой генератор у каждой порции
hits = 0
for _ in range(n_points):
x, y = rng.random(), rng.random()
if x * x + y * y <= 1.0:
hits += 1
return hits
def estimate_pi_serial(n_points):
return 4 * count_hits(n_points, seed=0) / n_points