Skip to content

Instantly share code, notes, and snippets.

@kwalrath
Forked from timsneath/montecarlo.dart
Last active March 23, 2022 01:10
Show Gist options
  • Save kwalrath/4d688b6e468fb4c53d312250f557ec5c to your computer and use it in GitHub Desktop.
Save kwalrath/4d688b6e468fb4c53d312250f557ec5c to your computer and use it in GitHub Desktop.
DartPad for dart.dev/overview
import 'dart:math' show Random;
main() async {
print('Compute π using the Monte Carlo method.');
await for (final estimate in computePi().take(100)) {
print('π ≅ $estimate');
}
}
/// Generates a stream of increasingly accurate estimates of π.
Stream<double> computePi({int batch: 100000}) async* {
var total = 0; // inferred to be of type int
var count = 0;
while (true) {
final points = generateRandom().take(batch);
final inside = points.where((p) => p.isInsideUnitCircle);
total += batch;
count += inside.length;
final ratio = count / total;
// Area of a circle is A = π⋅r², therefore π = A/r².
// So, when given random points with x ∈ <0,1>,
// y ∈ <0,1>, the ratio of those inside a unit circle
// should approach π / 4. Therefore, the value of π
// should be:
yield ratio * 4;
}
}
Iterable<Point> generateRandom([int? seed]) sync* {
final random = Random(seed);
while (true) {
yield Point(random.nextDouble(), random.nextDouble());
}
}
class Point {
final double x, y;
const Point(this.x, this.y);
bool get isInsideUnitCircle => x * x + y * y <= 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment