Skip to content

Conversation

@N-Dekker
Copy link
Contributor

@N-Dekker N-Dekker commented May 14, 2025

The original division of rand() by 32768.0 was probably meant to yield a number between [0, 1>. However, rand() returns an integer between [​0​, RAND_MAX], and RAND_MAX may not be 32767. In practice, when using GCC, RAND_MAX may be as large as 2147483647.

The "magic number" 32768.0 was introduced on 12 Jan 2002 already, with commit 074c74f.

The original division of `rand()` by `32768.0` was probably meant to yield a
number between `[0, 1>`. However, `rand()` returns an integer between
`[​0​, RAND_MAX]`, and `RAND_MAX` may not be `32767`. In practice, when using
GCC, `RAND_MAX` may be as large as `2147483647.

The "magic number" `32768.0` was introduced on 12 Jan 2002 already, with commit
074c74f.
@github-actions github-actions bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances area:Segmentation Issues affecting the Segmentation module labels May 14, 2025
@N-Dekker
Copy link
Contributor Author

@chentingpc Hi Ting Chen! Could it be that this is originally your code? If so, maybe you can have a look at this pull request! 😃

{
difenergy = energy[label] - energy[1 - label];
const double rand_num{ rand() / 32768.0 };
const double rand_num{ rand() / (RAND_MAX + 1.0) };
Copy link
Contributor Author

@N-Dekker N-Dekker May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Windows, using MSVC/Visual Studio 2022, RAND_MAX + 1.0 is exactly 32768.0, but on Linux, using GCC, RAND_MAX + 1.0 may be 2147483648.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context of this pull request:

const double rand_num{ rand() / 32768.0 };
double energy_num{ std::exp(static_cast<double>(difenergy * 0.5 * size / (2 * size - m_Temp))) };
if (rand_num < energy_num)
{
m_LabelledImage->SetPixel(offsetIndex3D, 1 - label);
}

So this rand_num is a random threshold for energy_num. On Linux + GCC, the numbers returned by rand() are usually much higher than on Windows + MSVC. So on Linux + GCC, the threshold will be much higher, making it less likely that the if (rand_num < energy_num) clause will be entered.

Copy link
Member

@dzenanz dzenanz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good on a glance.

Copy link
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@N-Dekker , wow, nice find!

@thewtex thewtex merged commit 43ea92d into InsightSoftwareConsortium:master May 15, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Segmentation Issues affecting the Segmentation module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants