Skip to content
Snippets Groups Projects
Commit f0806d5d 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 9edc017a
Branches
Tags
No related merge requests found
......@@ -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;
}
......
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