Skip to content

Commit b4f5d3f

Browse files
author
Giulia Baldini
committed
Change the way the random distribution returns the values to ensure compatibility between linux and msvc
1 parent 59c689a commit b4f5d3f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

bico/clustering/bico.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,32 @@ dimension(dim)
549549
maxVal.resize(L);
550550
double norm;
551551
std::normal_distribution<double> realDist(0.0, 1.0);
552+
553+
// TODO: Test this with MacOS and others
554+
#ifdef __GLIBCXX__
555+
# define getRandomValue() realDist(rg)
556+
#else
557+
// The order of the values in MSVC and LIBCPP is different.
558+
// To have the same results, we cache values pair-wise,
559+
// then return them in swapped order to put them in the same order.
560+
size_t i = 0;
561+
float vals[2] = { };
562+
auto getRandomValue = [&]() -> float {
563+
if (! (i % 2)) {
564+
vals[0] = realDist(rg);
565+
vals[1] = realDist(rg);
566+
}
567+
return vals[++i % 2];
568+
};
569+
#endif
570+
552571
for (int i = 0; i < L; i++)
553572
{
554573
maxVal[i] = -1;
555574
norm = 0.0;
556575
for (int j = 0; j < dimension; j++)
557576
{
558-
rndpoint[j] = realDist(rg);
577+
rndpoint[j] = getRandomValue();
559578
norm += rndpoint[j] * rndpoint[j];
560579
}
561580
norm = std::sqrt(norm);

0 commit comments

Comments
 (0)