Commit 51c48fa9 authored by Mark Olesen's avatar Mark Olesen
Browse files

BUG: non-uniform distribution for Random::position<label> (closes #865)

- use floor/truncate instead of round. Backport of changes in the
  develop-pre-release branch.
parent 4bafb6fd
......@@ -151,7 +151,24 @@ Foam::scalar Foam::Random::position
template<>
Foam::label Foam::Random::position(const label& start, const label& end)
{
return start + round(scalar01()*(end - start));
#ifdef FULLDEBUG
if (start > end)
{
FatalErrorInFunction
<< "start index " << start << " > end index " << end << nl
<< abort(FatalError);
}
#endif
// Extend the upper sampling range by 1 and floor the result.
// Since the range is non-negative, can use integer truncation
// instead using floor().
const label val = start + label(scalar01()*(end - start + 1));
// Rare case when scalar01() returns exactly 1.000 and the truncated
// value would be out of range.
return min(val, end);
}
......@@ -230,12 +247,12 @@ Foam::scalar Foam::Random::globalPosition
if (Pstream::master())
{
value = scalar01()*(end - start);
value = position<scalar>(start, end);
}
Pstream::scatter(value);
return start + value;
return value;
}
......@@ -250,12 +267,12 @@ Foam::label Foam::Random::globalPosition
if (Pstream::master())
{
value = round(scalar01()*(end - start));
value = position<label>(start, end);
}
Pstream::scatter(value);
return start + value;
return value;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment