sample method
Generates a random sample.
Example
import 'package:calc/calc.dart';
void main() {
final distribution = NormalDistribution(mean=0.0, variance:1.0);
final sample = distribution.sample();
}
Implementation
@override
double sample({Random? random}) {
random ??= Random();
// Box-Muller polar form algorithm
var x1 = 0.0;
var x2 = 0.0;
var w = 0.0;
do {
x1 = (2.0 * random.nextDouble()) - 1.0;
x2 = (2.0 * random.nextDouble()) - 1.0;
w = (x1 * x1) + (x2 * x2);
} while (w >= 1.0);
final r = x1 * sqrt((-2.0 * log(w)) / w);
return r * standardDeviation + mean;
}