File: Seed.hs

package info (click to toggle)
haskell-test-framework 0.6-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 176 kB
  • sloc: haskell: 928; makefile: 2
file content (32 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Test.Framework.Seed where

import Test.Framework.Utilities

import System.Random

import Data.Char


data Seed = FixedSeed Int
          | RandomSeed

instance Show Seed where
    show RandomSeed    = "random"
    show (FixedSeed n) = show n

instance Read Seed where
    readsPrec prec xs = if map toLower random_prefix == "random"
                        then [(RandomSeed, rest)]
                        else map (FixedSeed `onLeft`) (readsPrec prec xs)
      where (random_prefix, rest) = splitAt 6 xs

-- | Given a 'Seed', returns a new random number generator based on that seed and the
-- actual numeric seed that was used to build that generator, so it can be recreated.
newSeededStdGen :: Seed -> IO (StdGen, Int)
newSeededStdGen (FixedSeed seed) = return $ (mkStdGen seed, seed)
newSeededStdGen RandomSeed = newStdGenWithKnownSeed

newStdGenWithKnownSeed :: IO (StdGen, Int)
newStdGenWithKnownSeed = do
    seed <- randomIO
    return (mkStdGen seed, seed)