mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-23 00:22:56 +01:00
2 lines
No EOL
1.4 KiB
JavaScript
2 lines
No EOL
1.4 KiB
JavaScript
"use strict";(self.webpackChunkmy_application=self.webpackChunkmy_application||[]).push([[3648],{3648:(n,e,t)=>{t.r(e),t.d(e,{default:()=>i});const i="import 'dart:math' show Random;\n\nvoid main() async {\n print('Compute π using the Monte Carlo method.');\n await for (final estimate in computePi().take(100)) {\n print('π ≅ $estimate');\n }\n}\n\n/// Generates a stream of increasingly accurate estimates of π.\nStream<double> computePi({int batch = 100000}) async* {\n var total = 0; // Inferred to be of type int\n var count = 0;\n while (true) {\n final points = generateRandom().take(batch);\n final inside = points.where((p) => p.isInsideUnitCircle);\n\n total += batch;\n count += inside.length;\n final ratio = count / total;\n\n // Area of a circle is A = π⋅r², therefore π = A/r².\n // So, when given random points with x ∈ <0,1>,\n // y ∈ <0,1>, the ratio of those inside a unit circle\n // should approach π / 4. Therefore, the value of π\n // should be:\n yield ratio * 4;\n }\n}\n\nIterable<Point> generateRandom([int? seed]) sync* {\n final random = Random(seed);\n while (true) {\n yield Point(random.nextDouble(), random.nextDouble());\n }\n}\n\nclass Point {\n final double x;\n final double y;\n\n const Point(this.x, this.y);\n\n bool get isInsideUnitCircle => x * x + y * y <= 1;\n}\n"}}]);
|
|
//# sourceMappingURL=3648.js.map
|