Open in DartPad
import 'dart:async';
import 'dart:math' show Random;

main() async {
  print('Compute π using the Monte Carlo method.');
  await for (var estimate in computePi().take(500)) {
    print('π ≅ $estimate');

/// Generates a stream of increasingly accurate estimates of π.
Stream<double> computePi({int batch: 100000}) async* {
  var total = 0;
  var count = 0;
  while (true) {
    var points = generateRandom().take(batch);
    var inside = points.where((p) => p.isInsideUnitCircle);
    total += batch;
    count += inside.length;
    var 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 = new Random(seed);
  while (true) {
    yield new 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;

Dart helps you craft beautiful, high-quality experiences across all screens, with:

Get started Install Dart

Click the underlined text to learn more.

Language tour

Learn how to use Dart's major language features.


Use Dart to build mobile apps, web apps, and more.

Dart packages

Discover libraries and tools to help you build apps.

Core goals

Dart is an ambitious, long-term project. These are the core goals that drive our design decisions.

Provide a solid foundation of libraries and tools

A programming language is nothing without its core libraries and tools. We call it batteries included. Dart’s batteries have been powering very large apps for years now.

Make common programming tasks easy

Application programming comes with a set of common problems and common errors. Dart is designed to make those problems easier to tackle, and errors easier to catch. This is why we have async/await, generators, string interpolation, earlier error detection and much more.

Don’t surprise the programmer

There should be a direct correspondence between what you type and what’s going to happen. Magic (automatic type coercion, hoisting, “helper” globals, …) doesn’t mix well with large apps.

Be the stable, pragmatic solution for real apps

Dart might seem boring to some. We prefer the terms productive and stable. We work closely with our core customers—the developers who build large applications with Dart—to make sure we’re here for the long run.