-
-
Notifications
You must be signed in to change notification settings - Fork 717
BUG: Fix random number in RGBGibbsPriorFilter::GibbsTotalEnergy
#5350
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
BUG: Fix random number in RGBGibbsPriorFilter::GibbsTotalEnergy
#5350
Conversation
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.
|
@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) }; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
ITK/Modules/Segmentation/MarkovRandomFieldsClassifiers/include/itkRGBGibbsPriorFilter.hxx
Lines 292 to 297 in ab1ba0b
| 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.
dzenanz
left a comment
There was a problem hiding this 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.
thewtex
left a comment
There was a problem hiding this 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!
The original division of
rand()by32768.0was probably meant to yield a number between[0, 1>. However,rand()returns an integer between[0, RAND_MAX], andRAND_MAXmay not be32767. In practice, when using GCC,RAND_MAXmay be as large as2147483647.The "magic number"
32768.0was introduced on 12 Jan 2002 already, with commit 074c74f.