Skip to content

Commit 1e0ca92

Browse files
author
埃博拉酱
committed
Make Beta result equally possible to be 0 or 1 but not NaN when a and b are both very small
1 parent b9104ae commit 1e0ca92

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

src/Numerics/Distributions/Beta.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,13 @@ public IEnumerable<double> Samples()
392392
/// <returns>a random number from the Beta distribution.</returns>
393393
internal static double SampleUnchecked(System.Random rnd, double a, double b)
394394
{
395-
var x = Gamma.SampleUnchecked(rnd, a, 1.0);
396-
var y = Gamma.SampleUnchecked(rnd, b, 1.0);
397-
return x == 0 ? 0 : x / (x + y);
395+
double x, y;
396+
do
397+
{
398+
x = Gamma.SampleUnchecked(rnd, a, 1.0);
399+
y = Gamma.SampleUnchecked(rnd, b, 1.0);
400+
} while (x == 0 && y == 0);
401+
return x / (x + y);
398402
}
399403

400404
internal static void SamplesUnchecked(System.Random rnd, double[] values, double a, double b)

0 commit comments

Comments
 (0)