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,
32 import CPUTime (getCPUTime)
36 import PrelNum -- So we get fromInt, toInt
38 import PrelNumExtra ( float2Double, double2Float )
41 import Time (getClockTime, ClockTime(..))
43 import Char ( isSpace, chr, ord )
47 class RandomGen g where
57 instance RandomGen StdGen where
62 instance Show StdGen where
63 showsPrec p (StdGen s1 s2) =
68 instance Show StdGen where
69 showsPrec p (StdGen s1 s2) =
75 instance Read StdGen where
79 _ -> [stdFromString r] -- because it shouldn't ever fail.
82 (s1, r1) <- readDec (dropWhile isSpace r)
83 (s2, r2) <- readDec (dropWhile isSpace r1)
84 return (StdGen s1 s2, r2)
87 If we cannot unravel the StdGen from a string, create
88 one based on the string given.
90 stdFromString :: String -> (StdGen, String)
91 stdFromString s = (mkStdGen num, rest)
92 where (cs, rest) = splitAt 6 s
93 num = foldl (\a x -> x + 3 * a) 1 (map ord cs)
97 mkStdGen :: Int -> StdGen -- why not Integer ?
99 | s < 0 = mkStdGen (-s)
100 | otherwise = StdGen (s1+1) (s2+1)
102 (q, s1) = s `divMod` 2147483562
103 s2 = q `mod` 2147483398
105 createStdGen :: Integer -> StdGen
107 | s < 0 = createStdGen (-s)
108 | otherwise = StdGen (toInt (s1+1)) (toInt (s2+1))
110 (q, s1) = s `divMod` 2147483562
111 s2 = q `mod` 2147483398
115 The class definition - see library report for details.
119 -- Minimal complete definition: random and randomR
120 random :: RandomGen g => g -> (a, g)
121 randomR :: RandomGen g => (a,a) -> g -> (a,g)
123 randoms :: RandomGen g => g -> [a]
124 randoms g = x : randoms g' where (x,g') = random g
126 randomRs :: RandomGen g => (a,a) -> g -> [a]
127 randomRs ival g = x : randomRs ival g' where (x,g') = randomR ival g
130 randomIO = getStdRandom random
132 randomRIO :: (a,a) -> IO a
133 randomRIO range = getStdRandom (randomR range)
137 instance Random Int where
138 randomR (a,b) g = randomIvalInteger (toInteger a, toInteger b) g
139 random g = randomR (minBound,maxBound) g
141 instance Random Char where
143 case (randomIvalInteger (toInteger (ord a), toInteger (ord b)) g) of
145 random g = randomR (minBound,maxBound) g
147 instance Random Bool where
149 case (randomIvalInteger (toInteger (bool2Int a), toInteger (bool2Int b)) g) of
150 (x, g) -> (int2Bool x, g)
158 random g = randomR (minBound,maxBound) g
160 instance Random Integer where
161 randomR ival g = randomIvalInteger ival g
162 random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
164 instance Random Double where
165 randomR ival g = randomIvalDouble ival id g
166 random g = randomR (0::Double,1) g
168 -- hah, so you thought you were saving cycles by using Float?
171 instance Random Float where
172 random g = randomIvalDouble (0::Double,1) realToFrac g
173 randomR (a,b) g = randomIvalDouble (realToFrac a, realToFrac b) realToFrac g
175 instance Random Float where
176 randomR (a,b) g = randomIvalDouble (float2Double a, float2Double b) double2Float g
177 random g = randomIvalDouble (0::Double,1) double2Float g
185 mkStdRNG :: Integer -> IO StdGen
186 mkStdRNG o = return (createStdGen o)
188 mkStdRNG :: Integer -> IO StdGen
191 (TOD sec _) <- getClockTime
192 return (createStdGen (sec * 12345 + ct + o))
195 randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
196 randomIvalInteger (l,h) rng
197 | l > h = randomIvalInteger (h,l) rng
198 | otherwise = case (f n 1 rng) of (v, rng') -> (fromInteger (l + v `mod` k), rng')
209 f (n-1) (fromInt x + acc * b) g'
211 randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
212 randomIvalDouble (l,h) fromDouble rng
213 | l > h = randomIvalDouble (h,l) fromDouble rng
215 case (randomIvalInteger (toInteger (minBound::Int), toInteger (maxBound::Int)) rng) of
219 fromDouble ((l+h)/2) +
220 fromDouble ((h-l) / realToFrac intRange) *
221 fromIntegral (x::Int)
226 intRange = toInteger (maxBound::Int) - toInteger (minBound::Int)
228 iLogBase :: Integer -> Integer -> Integer
229 iLogBase b i = if i < b then 1 else 1 + iLogBase b (i `div` b)
231 stdNext :: StdGen -> (Int, StdGen)
232 stdNext (StdGen s1 s2) = (z', StdGen s1'' s2'')
233 where z' = if z < 1 then z + 2147483562 else z
237 s1' = 40014 * (s1 - k * 53668) - k * 12211
238 s1'' = if s1' < 0 then s1' + 2147483563 else s1'
241 s2' = 40692 * (s2 - k' * 52774) - k' * 3791
242 s2'' = if s2' < 0 then s2' + 2147483399 else s2'
245 stdSplit :: StdGen -> (StdGen, StdGen)
246 stdSplit std@(StdGen s1 s2)
249 -- no statistical foundation for this!
250 left = StdGen new_s1 t2
251 right = StdGen t1 new_s2
253 new_s1 | s1 == 2147483562 = 1
256 new_s2 | s2 == 1 = 2147483398
259 StdGen t1 t2 = snd (next std)
261 stdSplit :: StdGen -> (StdGen, StdGen)
262 stdSplit std@(StdGen s1 _) = (std, unsafePerformIO (mkStdRNG (fromInt s1)))
270 setStdGen :: StdGen -> IO ()
271 setStdGen sgen = writeIORef theStdGen sgen
273 getStdGen :: IO StdGen
274 getStdGen = readIORef theStdGen
276 theStdGen :: IORef StdGen
277 theStdGen = primRunST (newIORef (createStdGen 0))
281 global_rng :: MutableVar RealWorld StdGen
282 global_rng = unsafePerformIO $ do
286 setStdGen :: StdGen -> IO ()
287 setStdGen sgen = stToIO (writeVar global_rng sgen)
289 getStdGen :: IO StdGen
290 getStdGen = stToIO (readVar global_rng)
295 newStdGen :: IO StdGen
298 let (a,b) = split rng
302 getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
305 let (v, new_rng) = f rng