Добавить lec1/mc.py
This commit is contained in:
+12
@@ -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
|
||||
Reference in New Issue
Block a user