/
GYSFlash
/
MonteCarloApp
Обзор
Документация
Войти
/
GYSFlash
/
MonteCarloApp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/MonteCarloCalculator.java
39 строк
1 KB
Юрий
first_commit
17 июн 2025, 19:17
17 июн 2025, 19:17
ec7b4c9
Код
Авторство
О чём код?
package org.example; import java.util.Random; public class MonteCarloCalculator { private final Shape shape; private final double minX, maxX, minY, maxY; private final Random random = new Random(); public MonteCarloCalculator(Shape shape, double minX, double maxX, double minY, double maxY) { this.shape = shape; this.minX = minX; this.maxX = maxX; this.minY = minY; this.maxY = maxY; } public Result estimateArea(int n) { int countInside = 0; long start = System.currentTimeMillis(); for (int i = 0; i < n; i++) { double x = minX + random.nextDouble() * (maxX - minX); double y = minY + random.nextDouble() * (maxY - minY); Point p = new Point(x, y); if (shape.contains(p)) { countInside++; } } long duration = System.currentTimeMillis() - start; double rectArea = (maxX - minX) * (maxY - minY); double estimatedArea = ((double) countInside / n) * rectArea; double relativeError = Math.abs(estimatedArea - shape.exactArea()) / shape.exactArea() * 100.0; return new Result(n, estimatedArea, relativeError, duration); } public record Result(int samples, double estimatedArea, double relativeError, long timeMs) {} }