diff --git a/FastRng/Double/ShapeFitter.cs b/FastRng/Double/ShapeFitter.cs index dc8ee13..172c7d1 100644 --- a/FastRng/Double/ShapeFitter.cs +++ b/FastRng/Double/ShapeFitter.cs @@ -47,6 +47,9 @@ namespace FastRng.Double return x; var nextBucket = (int)Math.Floor(x * this.sampleSize); + if (nextBucket >= this.probabilities.Length) + nextBucket = this.probabilities.Length - 1; + var threshold = this.probabilities[nextBucket]; var y = await this.rng.NextNumber(0.0d, this.max, this.uniform, token); if (double.IsNaN(y)) diff --git a/FastRng/Float/ShapeFitter.cs b/FastRng/Float/ShapeFitter.cs index 025f583..899fd25 100644 --- a/FastRng/Float/ShapeFitter.cs +++ b/FastRng/Float/ShapeFitter.cs @@ -46,6 +46,9 @@ namespace FastRng.Float return x; var nextBucket = (int)MathF.Floor(x * this.sampleSize); + if (nextBucket >= this.probabilities.Length) + nextBucket = this.probabilities.Length - 1; + var threshold = this.probabilities[nextBucket]; var y = await this.rng.NextNumber(0.0f, this.max, this.uniform, token); if (float.IsNaN(y))