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.
This commit is contained in:
parent
4bafb6fd54
commit
51c48fa9c9
@ -151,7 +151,24 @@ Foam::scalar Foam::Random::position
|
|||||||
template<>
|
template<>
|
||||||
Foam::label Foam::Random::position(const label& start, const label& end)
|
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())
|
if (Pstream::master())
|
||||||
{
|
{
|
||||||
value = scalar01()*(end - start);
|
value = position<scalar>(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pstream::scatter(value);
|
Pstream::scatter(value);
|
||||||
|
|
||||||
return start + value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -250,12 +267,12 @@ Foam::label Foam::Random::globalPosition
|
|||||||
|
|
||||||
if (Pstream::master())
|
if (Pstream::master())
|
||||||
{
|
{
|
||||||
value = round(scalar01()*(end - start));
|
value = position<label>(start, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pstream::scatter(value);
|
Pstream::scatter(value);
|
||||||
|
|
||||||
return start + value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user