generate method

List<num> generate(
  1. int n, {
  2. int? seed,
})

Generates random data drawn from a normal distribution.

Produces a pseudorandom sample drawn from a normal distribution through the Box-Muller algorithm.

Implementation

List<num> generate(int n, {int? seed}) {
  final rand = seed == null ? math.Random() : math.Random(seed),
      standardDeviation = math.sqrt(variance);
  num next() {
    final u1 = rand.nextDouble(),
        u2 = rand.nextDouble(),
        r = math.sqrt(-2 * math.log(u1)),
        t = 2 * math.pi * u2;
    return r * math.cos(t);
  }

  final zScores = [for (var _ = 0; _ < n; _++) next()];

  return [for (final z in zScores) z * standardDeviation + mean];
}