Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for 0 stddev when generating a random number using normal distribution #615

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Rand.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ double Rand::DblUniform(double _min, double _max)
//////////////////////////////////////////////////
double Rand::DblNormal(double _mean, double _sigma)
{
if (equal(_sigma, 0.0))
iche033 marked this conversation as resolved.
Show resolved Hide resolved
return _mean;

NormalRealDist d(_mean, _sigma);
return d(RandGenerator());
}
Expand All @@ -69,6 +72,9 @@ int32_t Rand::IntUniform(int _min, int _max)
//////////////////////////////////////////////////
int32_t Rand::IntNormal(int _mean, int _sigma)
{
if (_sigma == 0)
return _mean;

NormalRealDist d(_mean, _sigma);

return static_cast<int32_t>(d(RandGenerator()));
Expand Down
7 changes: 7 additions & 0 deletions src/Rand_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ TEST(RandTest, Rand)
EXPECT_EQ(i, 9);
EXPECT_NEAR(d, 3.00618, 1e-5);
#endif

// Test with sigma == 0
d = math::Rand::DblNormal(2.0, 0.0);
i = math::Rand::IntNormal(10, 0);

EXPECT_NEAR(2.0, d, 1e-6);
EXPECT_EQ(10, i);
}
#endif
}
Expand Down
Loading