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

improve: normal random skew towards means #1672

Merged
merged 7 commits into from
Nov 13, 2023
Merged
Changes from all 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
22 changes: 6 additions & 16 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,23 +398,13 @@

int32_t normal_random(int32_t minNumber, int32_t maxNumber) {
static std::normal_distribution<float> normalRand(0.5f, 0.25f);
if (minNumber == maxNumber) {
return minNumber;
} else if (minNumber > maxNumber) {
std::swap(minNumber, maxNumber);
}
float v;
do {
v = normalRand(getRandomGenerator());
} while (v < 0.0 || v > 1.0);

int32_t increment;
const int32_t diff = maxNumber - minNumber;
const float v = normalRand(getRandomGenerator());
if (v < 0.0) {
increment = diff / 2;
} else if (v > 1.0) {
increment = (diff + 1) / 2;
} else {
increment = round(v * diff);
}
return minNumber + increment;
auto &&[a, b] = std::minmax(minNumber, maxNumber);

Check warning on line 406 in src/utils/tools.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

[cppcheck] reported by reviewdog 🐶 Variable 'a' is not assigned a value. Raw Output: src/utils/tools.cpp:406:Variable 'a' is not assigned a value.
return a + std::lround(v * (b - a));
}

bool boolean_random(double probability /* = 0.5*/) {
Expand Down
Loading