samplePdf function

List<num> samplePdf(
  1. int n,
  2. num min,
  3. num max,
  4. num probDistMax,
  5. ProbabilityDensity pdf,
  6. {int seed}
)

Returns a random sample with probability density probabilityDensity.

  • n: sample size,
  • xMin: lower limit of the sample values,
  • xMax: upper limit of the sample values,
  • yMax: maximum value of probDistFunc,
  • seed: optional random generator seed.

Implementation

List<num> samplePdf(
  int n,
  num min,
  num max,
  num probDistMax,
  ProbabilityDensity pdf, {
  int? seed,
}) {
  final result = <num>[];
  final random = Random(seed);

  final range = max - min;

  while (result.length < n) {
    final x = range * random.nextDouble() + min;
    final y = probDistMax * random.nextDouble();

    if (y < pdf(x)) {
      result.add(x);
    }
  }
  return result;
}