Commit d8e406fd authored by Johan Kiviniemi's avatar Johan Kiviniemi
Browse files

mkStdGen32: Avoid infinite recursion when applied to minBound

"mkStdGen32 s" would call itself with "negate s" if "s" is negative. Alas,
(minBound :: Int32) == negate minBound. "mkStdGen32 minBound" would thus
recurse infinitely.

Compute "sMaybeNegative .&. maxBound" instead. That clears the most significant
bit in signed types.
parent b78ab23d
......@@ -68,6 +68,7 @@ module System.Random
import Prelude
import Data.Bits
import Data.Int
import Data.Word
import Foreign.C.Types
......@@ -218,10 +219,9 @@ mkStdGen :: Int -> StdGen -- why not Integer ?
mkStdGen s = mkStdGen32 $ fromIntegral s
mkStdGen32 :: Int32 -> StdGen
mkStdGen32 s
| s < 0 = mkStdGen32 (-s)
| otherwise = StdGen (s1+1) (s2+1)
mkStdGen32 sMaybeNegative = StdGen (s1+1) (s2+1)
where
s = sMaybeNegative .&. maxBound
(q, s1) = s `divMod` 2147483562
s2 = q `mod` 2147483398
......
Markdown is supported
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