From fa029fc1c3dbeede67e48fcda6a63932395a1adb Mon Sep 17 00:00:00 2001 From: Pharap Date: Tue, 3 Oct 2017 14:43:23 +0100 Subject: [PATCH] Fix bug in randomSFixed --- src/FixedPoints/Utils.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/FixedPoints/Utils.h b/src/FixedPoints/Utils.h index 4c07250..3d1ead2 100644 --- a/src/FixedPoints/Utils.h +++ b/src/FixedPoints/Utils.h @@ -203,7 +203,7 @@ template< unsigned Integer, unsigned Fraction > UFixed randomUFixed(const UFixed & inclusiveLowerBound, const UFixed & exclusiveUpperBound) { using InternalType = typename UFixed::InternalType; - return UFixed::fromInternal(inclusiveLowerBound.getInternal() + FIXED_POINTS_DETAILS::RandomHelper::Random() % exclusiveUpperBound.getInternal()); + return UFixed::fromInternal(inclusiveLowerBound.getInternal() + (FIXED_POINTS_DETAILS::RandomHelper::Random() % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); } // @@ -228,6 +228,7 @@ template< unsigned Integer, unsigned Fraction > SFixed randomSFixed(const SFixed & inclusiveLowerBound, const SFixed & exclusiveUpperBound) { using InternalType = typename SFixed::InternalType; - return SFixed::fromInternal(inclusiveLowerBound.getInternal() + FIXED_POINTS_DETAILS::RandomHelper::Random() % exclusiveUpperBound.getInternal()); + auto value = FIXED_POINTS_DETAILS::RandomHelper::Random(); + return SFixed::fromInternal(inclusiveLowerBound.getInternal() + (abs(value) % (exclusiveUpperBound.getInternal() - inclusiveLowerBound.getInternal()))); } FIXED_POINTS_END_NAMESPACE