From e9eade40228adb5d7f4168082612d89d1528d5f9 Mon Sep 17 00:00:00 2001 From: Leonhard Markert Date: Thu, 18 Jun 2020 19:06:35 +0000 Subject: [PATCH] Use toIntegralSized (#167) --- src/System/Random/Internal.hs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/System/Random/Internal.hs b/src/System/Random/Internal.hs index 554e99fbe..86ea77aca 100644 --- a/src/System/Random/Internal.hs +++ b/src/System/Random/Internal.hs @@ -867,13 +867,12 @@ uniformIntegralM :: (Bits a, Integral a, StatefulGen g m) => (a, a) -> g -> m a uniformIntegralM (l, h) gen = case l `compare` h of LT -> do let limit = h - l - let limitAsWord64 :: Word64 = fromIntegral limit - bounded <- - if fromIntegral limitAsWord64 == limit + bounded <- case toIntegralSized limit :: Maybe Word64 of + Just limitAsWord64 -> -- Optimisation: if 'limit' fits into 'Word64', generate a bounded -- 'Word64' and then convert to 'Integer' - then fromIntegral <$> unsignedBitmaskWithRejectionM uniformWord64 limitAsWord64 gen - else boundedExclusiveIntegralM (limit + 1) gen + fromIntegral <$> unsignedBitmaskWithRejectionM uniformWord64 limitAsWord64 gen + Nothing -> boundedExclusiveIntegralM (limit + 1) gen return $ l + bounded GT -> uniformIntegralM (h, l) gen EQ -> pure l