@@ -22,14 +22,15 @@ module Test.QuickCheck.Gen
2222 , evalGen
2323 , perturbGen
2424 , uniform
25- , showSample
26- , showSample'
25+ , sample
26+ , randomSample
27+ , randomSample'
2728 ) where
2829
2930import Prelude
3031
3132import Control.Monad.Eff (Eff ())
32- import Control.Monad.Eff.Console ( CONSOLE (), print )
33+ import Control.Monad.Eff.Random ( RANDOM () )
3334import Data.Array ((!!), length , range )
3435import Data.Foldable (fold )
3536import Data.Int (fromNumber , toNumber )
@@ -148,21 +149,23 @@ evalGen :: forall a. Gen a -> GenState -> a
148149evalGen gen st = (runGen gen st).value
149150
150151-- | Sample a random generator
151- sample :: forall r a . Size -> Gen a -> Array a
152- sample sz g = evalGen (vectorOf sz g) { newSeed: zero , size: sz }
152+ sample :: forall r a . Seed -> Size -> Gen a -> Array a
153+ sample seed sz g = evalGen (vectorOf sz g) { newSeed: seed , size: sz }
153154
154- -- | Print a random sample to the console
155- showSample' :: forall r a . (Show a ) => Size -> Gen a -> Eff (console :: CONSOLE | r ) Unit
156- showSample' n g = print $ sample n g
155+ -- | Sample a random generator, using a randomly generated seed
156+ randomSample' :: forall r a . Size -> Gen a -> Eff (random :: RANDOM | r ) (Array a )
157+ randomSample' n g = do
158+ seed <- randomSeed
159+ return $ sample seed n g
157160
158- -- | Print a random sample of 10 values to the console
159- showSample :: forall r a . ( Show a ) => Gen a -> Eff (console :: CONSOLE | r ) Unit
160- showSample = showSample ' 10
161+ -- | Get a random sample of 10 values
162+ randomSample :: forall r a . Gen a -> Eff (random :: RANDOM | r ) ( Array a )
163+ randomSample = randomSample ' 10
161164
162165-- | A random generator which simply outputs the current seed
163166lcgStep :: Gen Int
164167lcgStep = Gen f where
165- f s = { value: s.newSeed, state: s { newSeed = lcgNext s.newSeed } }
168+ f s = { value: runSeed s.newSeed, state: s { newSeed = lcgNext s.newSeed } }
166169
167170-- | A random generator which approximates a uniform random variable on `[0, 1]`
168171uniform :: Gen Number
@@ -172,7 +175,9 @@ foreign import float32ToInt32 :: Number -> Int
172175
173176-- | Perturb a random generator by modifying the current seed
174177perturbGen :: forall a . Number -> Gen a -> Gen a
175- perturbGen n (Gen f) = Gen $ \s -> f (s { newSeed = lcgNext (float32ToInt32 n) + s.newSeed })
178+ perturbGen n (Gen f) = Gen $ \s -> f (s { newSeed = perturb s.newSeed })
179+ where
180+ perturb oldSeed = mkSeed (runSeed (lcgNext (mkSeed (float32ToInt32 n))) + runSeed oldSeed)
176181
177182instance functorGen :: Functor Gen where
178183 map f (Gen g) = Gen $ \s -> case g s of
0 commit comments