randomGeometricSource function
Returns a randomGeometric function but where the given random number
generator source
is used as the source of randomness instead of
Random.nextDouble.
The given random number generator must implement the same interface as Random.nextDouble and only return values in the range [0, 1). This is useful when a seeded random number generator is preferable to Random.nextDouble.
final seed = …; // any number in [0, 1)
final random = randomGeometricSource(randomLcg(seed))(…);
Implementation
num Function() Function(num) randomGeometricSource(num Function() source) {
return (num p) {
if (p < 0 || p > 1) throw RangeError.range(p, 0, 1, "p");
if (p == 0) return () => double.infinity;
if (p == 1) return () => 1;
p = log(1 - p);
return () {
return 1 + (log(1 - source()) / p).floor();
};
};
}