Random(seed) from dart:math is a pseudo-random number generator. Given the same seed, it produces the same sequence of nextInt, nextDouble, and nextBool results across runs, which keeps tests, demos, and offline replay deterministic. The page draws two nextInt(100) values, a nextDouble, and a nextBool from a Random(42), formats the double for output, then builds a fresh Random(42) and confirms its first value matches the original.

Program

Play the program to draw a fixed sequence from Random(42), format the double, then verify that a brand-new Random(42) reproduces the first value.

random_seeded.dart
Replay: real traced execution (multi-file project)
import 'dart:math';

void main() {
  var rng = Random(42);
  var n1 = rng.nextInt(100);
  var n2 = rng.nextInt(100);
  var d = rng.nextDouble();
  var shown = d.toStringAsFixed(4);
  var flip = rng.nextBool();
  var again = Random(42).nextInt(100);
  print('$n1 $n2 $shown $flip again=$again');
}
  1. rng ← Random(seed: 42)

    3void main() {4  var rng = Random(42);5  var n1 = rng.nextInt(100);
    values this stepRandom(seed: 42)rng
  2. n1 ← 87 (int)

    4var rng = Random(42);5var n1 = rng.nextInt(100);6var n2 = rng.nextInt(100);
    values this step87 (int)n1
  3. n2 ← 58 (int)

    5var n1 = rng.nextInt(100);6var n2 = rng.nextInt(100);7var d = rng.nextDouble();
    values this step58 (int)n2
  4. d ← 0.6041479725361217 (double)

    6var n2 = rng.nextInt(100);7var d = rng.nextDouble();8var shown = d.toStringAsFixed(4);
    values this step0.6041479725361217 (double)d
  5. shown ← 0.6041 (String)

    7var d = rng.nextDouble();8var shown = d.toStringAsFixed(4);9var flip = rng.nextBool();
    values this step0.6041 (String)shown0.6041479725361217d
  6. flip ← false (bool)

    8var shown = d.toStringAsFixed(4);9var flip = rng.nextBool();10var again = Random(42).nextInt(100);
    values this stepfalse (bool)flip
  7. again ← 87 (int, fresh seed=42)

    9var flip = rng.nextBool();10var again = Random(42).nextInt(100);11print('$n1 $n2 $shown $flip again=$again');
    values this step87 (int, fresh seed=42)again
  8. print('$n1 $n2 $shown $flip again=$again');

    10  var again = Random(42).nextInt(100);11  print('$n1 $n2 $shown $flip again=$again');12}
    output87 58 0.6041 false again=87
    values this step87n158n20.6041shownfalseflip87again

Follow the Seed

  1. Random(42) starts the fixed sequence.
  2. The first two nextInt(100) values are 87 and 58.
  3. The next double is 0.6041479725361217, shown as 0.6041.
  4. The next boolean is false.
  5. A fresh Random(42) starts over, so again is 87. | draw | value | | --- | --- | | n1 | 87 | | n2 | 58 | | shown | 0.6041 | | flip | false | | again | 87 |
seeded Random `Random(seed)` is a pseudo-random generator. The full sequence of `nextInt`, `nextDouble`, and `nextBool` results is determined by the seed.
reproducibility A fresh `Random(42)` returns the same first `nextInt(100)` (here `87`) as the original instance, so tests and demos stay deterministic.
nextInt nextDouble nextBool `nextInt(n)` returns `0..n-1`; `nextDouble()` returns a value in `[0.0, 1.0)`; `nextBool()` returns `true` or `false`. Each random call advances the generator's internal state.
formatting `toStringAsFixed(4)` formats the double draw as a `String` for short output; the original `double` value remains unchanged.

Exercise: random_seeded.dart

Reproduce 87 58 0.6041 false again=87, then identify why again matches the first draw.