1 -----------------------------------------------------------------------------
3 -- Module : System.Random
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/core/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- $Id: Random.hs,v 1.2 2001/12/21 15:07:26 simonmar Exp $
15 -----------------------------------------------------------------------------
19 RandomGen(next, split, genRange)
22 , Random ( random, randomR,
31 -- The June 1988 (v31 #6) issue of the Communications of the ACM has an
32 -- article by Pierre L'Ecuyer called, "Efficient and Portable Combined
33 -- Random Number Generators". Here is the Portable Combined Generator of
34 -- L'Ecuyer for 32-bit computers. It has a period of roughly 2.30584e18.
36 -- Transliterator: Lennart Augustsson
38 -- sof 1/99 - code brought (kicking and screaming) into the new Random
43 import System.CPUTime ( getCPUTime )
44 import Data.Char ( isSpace, chr, ord )
45 import System.IO.Unsafe ( unsafePerformIO )
48 #ifdef __GLASGOW_HASKELL__
49 import GHC.Show ( showSignedInt, showSpace )
50 import GHC.Read ( readDec )
51 import GHC.IOBase ( unsafePerformIO, stToIO )
52 import System.Time ( getClockTime, ClockTime(..) )
55 class RandomGen g where
58 genRange :: g -> (Int,Int)
61 genRange g = (minBound,maxBound)
67 instance RandomGen StdGen where
71 #ifdef __GLASGOW_HASKELL__
72 instance Show StdGen where
73 showsPrec p (StdGen s1 s2) =
80 instance Show StdGen where
81 showsPrec p (StdGen s1 s2) =
87 instance Read StdGen where
91 _ -> [stdFromString r] -- because it shouldn't ever fail.
94 (s1, r1) <- readDec (dropWhile isSpace r)
95 (s2, r2) <- readDec (dropWhile isSpace r1)
96 return (StdGen s1 s2, r2)
99 If we cannot unravel the StdGen from a string, create
100 one based on the string given.
102 stdFromString :: String -> (StdGen, String)
103 stdFromString s = (mkStdGen num, rest)
104 where (cs, rest) = splitAt 6 s
105 num = foldl (\a x -> x + 3 * a) 1 (map ord cs)
108 mkStdGen :: Int -> StdGen -- why not Integer ?
110 | s < 0 = mkStdGen (-s)
111 | otherwise = StdGen (s1+1) (s2+1)
113 (q, s1) = s `divMod` 2147483562
114 s2 = q `mod` 2147483398
116 createStdGen :: Integer -> StdGen
118 | s < 0 = createStdGen (-s)
119 | otherwise = StdGen (fromInteger (s1+1)) (fromInteger (s2+1))
121 (q, s1) = s `divMod` 2147483562
122 s2 = q `mod` 2147483398
125 -- The class definition - see library report for details.
128 -- Minimal complete definition: random and randomR
129 random :: RandomGen g => g -> (a, g)
130 randomR :: RandomGen g => (a,a) -> g -> (a,g)
132 randoms :: RandomGen g => g -> [a]
133 randoms g = x : randoms g' where (x,g') = random g
135 randomRs :: RandomGen g => (a,a) -> g -> [a]
136 randomRs ival g = x : randomRs ival g' where (x,g') = randomR ival g
139 randomIO = getStdRandom random
141 randomRIO :: (a,a) -> IO a
142 randomRIO range = getStdRandom (randomR range)
145 instance Random Int where
146 randomR (a,b) g = randomIvalInteger (toInteger a, toInteger b) g
147 random g = randomR (minBound,maxBound) g
149 instance Random Char where
151 case (randomIvalInteger (toInteger (ord a), toInteger (ord b)) g) of
153 random g = randomR (minBound,maxBound) g
155 instance Random Bool where
157 case (randomIvalInteger (toInteger (bool2Int a), toInteger (bool2Int b)) g) of
158 (x, g) -> (int2Bool x, g)
166 random g = randomR (minBound,maxBound) g
168 instance Random Integer where
169 randomR ival g = randomIvalInteger ival g
170 random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
172 instance Random Double where
173 randomR ival g = randomIvalDouble ival id g
174 random g = randomR (0::Double,1) g
176 -- hah, so you thought you were saving cycles by using Float?
177 instance Random Float where
178 random g = randomIvalDouble (0::Double,1) realToFrac g
179 randomR (a,b) g = randomIvalDouble (realToFrac a, realToFrac b) realToFrac g
181 #ifdef __GLASGOW_HASKELL__
182 mkStdRNG :: Integer -> IO StdGen
185 (TOD sec _) <- getClockTime
186 return (createStdGen (sec * 12345 + ct + o))
190 mkStdRNG :: Integer -> IO StdGen
193 return (createStdGen (ct + o))
196 randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
197 randomIvalInteger (l,h) rng
198 | l > h = randomIvalInteger (h,l) rng
199 | otherwise = case (f n 1 rng) of (v, rng') -> (fromInteger (l + v `mod` k), rng')
210 f (n-1) (fromIntegral x + acc * b) g'
212 randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
213 randomIvalDouble (l,h) fromDouble rng
214 | l > h = randomIvalDouble (h,l) fromDouble rng
216 case (randomIvalInteger (toInteger (minBound::Int), toInteger (maxBound::Int)) rng) of
220 fromDouble ((l+h)/2) +
221 fromDouble ((h-l) / realToFrac intRange) *
222 fromIntegral (x::Int)
227 intRange = toInteger (maxBound::Int) - toInteger (minBound::Int)
229 iLogBase :: Integer -> Integer -> Integer
230 iLogBase b i = if i < b then 1 else 1 + iLogBase b (i `div` b)
232 stdNext :: StdGen -> (Int, StdGen)
233 stdNext (StdGen s1 s2) = (z', StdGen s1'' s2'')
234 where z' = if z < 1 then z + 2147483562 else z
238 s1' = 40014 * (s1 - k * 53668) - k * 12211
239 s1'' = if s1' < 0 then s1' + 2147483563 else s1'
242 s2' = 40692 * (s2 - k' * 52774) - k' * 3791
243 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)
262 setStdGen :: StdGen -> IO ()
263 setStdGen sgen = writeIORef theStdGen sgen
265 getStdGen :: IO StdGen
266 getStdGen = readIORef theStdGen
268 theStdGen :: IORef StdGen
269 theStdGen = unsafePerformIO (newIORef (createStdGen 0))
271 newStdGen :: IO StdGen
274 let (a,b) = split rng
278 getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
281 let (v, new_rng) = f rng