Skip to content

Commit

Permalink
Use toIntegralSized (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
curiousleo authored Jun 18, 2020
1 parent bd9613c commit e9eade4
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9eade4

Please sign in to comment.