randomSample function

List<double> randomSample(
  1. int sampleSize,
  2. num xMin,
  3. num xMax,
  4. num yMax,
  5. ProbabilityDensity pdf, {
  6. int? seed,
})

Returns a random sample with probability density probabilityDensity.

  • sampleSize: sample size,
  • xMin: lower limit of the sample values,
  • xMax: upper limit of the sample values,
  • yMax: maximum value of pdf,
  • pdf: probability density function,
  • seed: optional random generator seed.

The generator uses a rejection sampling algorithm.

Implementation

List<double> randomSample(
  int sampleSize,
  num xMin,
  num xMax,
  num yMax,
  ProbabilityDensity pdf, {
  int? seed,
}) {
  final result = <double>[];
  final random = Random(seed);
  final range = xMax - xMin;
  while (result.length < sampleSize) {
    final x = range * random.nextDouble() + xMin;
    final y = yMax * random.nextDouble();
    if (y < pdf(x)) {
      result.add(x);
    }
  }
  return result;
}