2 % (c) The GRASP/AQUA Project, Glasgow University, 1995-99
6 The June 1988 (v31 #6) issue of the Communications of the ACM has an
7 article by Pierre L'Ecuyer called, "Efficient and Portable Combined
8 Random Number Generators". Here is the Portable Combined Generator of
9 L'Ecuyer for 32-bit computers. It has a period of roughly 2.30584e18.
11 Transliterator: Lennart Augustsson
13 sof 1/99 - code brought (kicking and screaming) into the new Random
19 RandomGen(next, split)
22 , Random ( random, randomR,
31 import CPUTime (getCPUTime)
35 import PrelNum -- So we get fromInt, toInt
37 import PrelNumExtra ( float2Double, double2Float )
40 import Char ( isSpace, chr, ord )
41 import Time (getClockTime, ClockTime(..))
46 class RandomGen g where
56 instance RandomGen StdGen where
60 instance Show StdGen where
61 showsPrec p (StdGen s1 s2) =
66 instance Read StdGen where
70 _ -> [stdFromString r] -- because it shouldn't ever fail.
73 (s1, r1) <- readDec (dropWhile isSpace r)
74 (s2, r2) <- readDec (dropWhile isSpace r1)
75 return (StdGen s1 s2, r2)
78 If we cannot unravel the StdGen from a string, create
79 one based on the string given.
81 stdFromString :: String -> (StdGen, String)
82 stdFromString s = (mkStdGen num, rest)
83 where (cs, rest) = splitAt 6 s
84 num = foldl (\a x -> x + 3 * a) 1 (map ord cs)
88 mkStdGen :: Int -> StdGen -- why not Integer ?
90 | s < 0 = mkStdGen (-s)
91 | otherwise = StdGen (s1+1) (s2+1)
93 (q, s1) = s `divMod` 2147483562
94 s2 = q `mod` 2147483398
96 createStdGen :: Integer -> StdGen
98 | s < 0 = createStdGen (-s)
99 | otherwise = StdGen (toInt (s1+1)) (toInt (s2+1))
101 (q, s1) = s `divMod` 2147483562
102 s2 = q `mod` 2147483398
106 The class definition - see library report for details.
110 -- Minimal complete definition: random and randomR
111 random :: RandomGen g => g -> (a, g)
112 randomR :: RandomGen g => (a,a) -> g -> (a,g)
114 randoms :: RandomGen g => g -> [a]
115 randoms g = x : randoms g' where (x,g') = random g
117 randomRs :: RandomGen g => (a,a) -> g -> [a]
118 randomRs ival g = x : randomRs ival g' where (x,g') = randomR ival g
121 randomIO = getStdRandom random
123 randomRIO :: (a,a) -> IO a
124 randomRIO range = getStdRandom (randomR range)
128 instance Random Int where
129 randomR (a,b) g = randomIvalInteger (toInteger a, toInteger b) g
130 random g = randomR (minBound,maxBound) g
132 instance Random Char where
134 case (randomIvalInteger (toInteger (ord a), toInteger (ord b)) g) of
136 random g = randomR (minBound,maxBound) g
138 instance Random Bool where
140 case (randomIvalInteger (toInteger (bool2Int a), toInteger (bool2Int b)) g) of
141 (x, g) -> (int2Bool x, g)
149 random g = randomR (minBound,maxBound) g
151 instance Random Integer where
152 randomR ival g = randomIvalInteger ival g
153 random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
155 instance Random Double where
156 randomR ival g = randomIvalDouble ival id g
157 random g = randomR (0::Double,1) g
159 -- hah, so you thought you were saving cycles by using Float?
160 instance Random Float where
161 randomR (a,b) g = randomIvalDouble (float2Double a, float2Double b) double2Float g
162 random g = randomIvalDouble (0::Double,1) double2Float g
168 mkStdRNG :: Integer -> IO StdGen
171 (TOD sec _) <- getClockTime
172 return (createStdGen (sec * 12345 + ct + o))
174 randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
175 randomIvalInteger (l,h) rng
176 | l > h = randomIvalInteger (h,l) rng
177 | otherwise = case (f n 1 rng) of (v, rng') -> (fromInteger (l + v `mod` k), rng')
188 f (n-1) (fromInt x + acc * b) g'
190 randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
191 randomIvalDouble (l,h) fromDouble rng
192 | l > h = randomIvalDouble (h,l) fromDouble rng
194 case (randomIvalInteger (toInteger (minBound::Int), toInteger (maxBound::Int)) rng) of
198 fromDouble ((l+h)/2) +
199 fromDouble ((h-l) / realToFrac intRange) *
200 fromIntegral (x::Int)
205 intRange = toInteger (maxBound::Int) - toInteger (minBound::Int)
207 iLogBase :: Integer -> Integer -> Integer
208 iLogBase b i = if i < b then 1 else 1 + iLogBase b (i `div` b)
210 stdNext :: StdGen -> (Int, StdGen)
211 stdNext (StdGen s1 s2) = (z', StdGen s1'' s2'')
212 where z' = if z < 1 then z + 2147483562 else z
216 s1' = 40014 * (s1 - k * 53668) - k * 12211
217 s1'' = if s1' < 0 then s1' + 2147483563 else s1'
220 s2' = 40692 * (s2 - k' * 52774) - k' * 3791
221 s2'' = if s2' < 0 then s2' + 2147483399 else s2'
223 stdSplit :: StdGen -> (StdGen, StdGen)
224 stdSplit std@(StdGen s1 _) = (std, unsafePerformIO (mkStdRNG (fromInt s1)))
230 global_rng :: MutableVar RealWorld StdGen
231 global_rng = unsafePerformIO $ do
235 setStdGen :: StdGen -> IO ()
236 setStdGen sgen = stToIO (writeVar global_rng sgen)
238 getStdGen :: IO StdGen
239 getStdGen = stToIO (readVar global_rng)
241 newStdGen :: IO StdGen
244 let (a,b) = split rng
248 getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
251 let (v, new_rng) = f rng