rando 0.0.0.2 → 0.0.0.3
raw patch · 4 files changed
+44/−15 lines, 4 filesPVP: minor bump suggested
API additions: PVP suggests at least a minor version bump
API changes (from Hackage documentation)
+ System.Random.Pick: flipCoin' :: TFGen -> (Bool, TFGen)
+ System.Random.Pick: pickOne' :: [x] -> TFGen -> (x, TFGen)
+ System.Random.Rando.Internal: inIO :: (TFGen -> (x, TFGen)) -> IO x
+ System.Random.Shuffle.FisherYates: shuffle' :: [x] -> TFGen -> ([x], TFGen)
Files
- rando.cabal +2/−1
- src/System/Random/Pick.hs +18/−7
- src/System/Random/Rando/Internal.hs +12/−0
- src/System/Random/Shuffle/FisherYates.hs +12/−7
rando.cabal view
@@ -1,5 +1,5 @@ name: rando-version: 0.0.0.2+version: 0.0.0.3 synopsis: Easy-to-use randomness for livecoding description: Easy-to-use randomness for livecoding.@@ -32,6 +32,7 @@ Rando , System.Random.Pick , System.Random.Shuffle.FisherYates+ , System.Random.Rando.Internal -- other-modules: -- other-extensions: build-depends:
src/System/Random/Pick.hs view
@@ -1,17 +1,28 @@ module System.Random.Pick ( pickOne , flipCoin++ -- * \"Internal\" functions+ , pickOne'+ , flipCoin' ) where -import System.Random.TF.Init-import System.Random.TF.Instances+import System.Random.TF.Gen (TFGen)+import System.Random.TF.Instances (randomR) +import System.Random.Rando.Internal (inIO)+ pickOne :: [x] -> IO x-pickOne l = do- g <- newTFGen- let (ix, _) = randomR (0, (length::[a]->Int) l - 1) g- pure $ l !! ix+pickOne l = inIO $ pickOne' l +pickOne' :: [x] -> TFGen -> (x, TFGen)+pickOne' l g0 =+ let (ix, g1) =+ randomR (0, (length::[a]->Int) l - 1) g0+ in (l !! ix, g1)+ flipCoin :: IO Bool-flipCoin = pickOne [True, False]+flipCoin = inIO $ flipCoin' +flipCoin' :: TFGen -> (Bool, TFGen)+flipCoin' = pickOne' [True, False]
+ src/System/Random/Rando/Internal.hs view
@@ -0,0 +1,12 @@+module System.Random.Rando.Internal (+ inIO+ ) where++-- import System.Random (RandomGen)+import System.Random.TF++inIO :: (TFGen -> (x, TFGen)) -> IO x+inIO f = do+ g <- newTFGen+ let (x, _) = f g+ pure x
src/System/Random/Shuffle/FisherYates.hs view
@@ -1,5 +1,6 @@ module System.Random.Shuffle.FisherYates ( shuffle+ , shuffle' ) where import Control.Monad@@ -10,14 +11,17 @@ import System.Random.TF import System.Random.TF.Instances --- The Fisher-Yates shuffle, asymptotically optimal for both space and time:+import System.Random.Rando.Internal (inIO)+ shuffle :: [x] -> IO [x]-shuffle list = do+shuffle l = inIO $ shuffle' l - initGen <- newTFGen+-- The Fisher-Yates shuffle, asymptotically optimal for both space and time:+shuffle' :: [x] -> TFGen -> ([x], TFGen)+shuffle' list gen0 = - pure $ V.toList $ runST $ do- genVar <- newSTRef initGen+ runST $ do+ genVar <- newSTRef gen0 v <- V.thaw $ V.fromList list let vLen = VM.length v@@ -28,8 +32,9 @@ VM.unsafeSwap v indexToSwap n -- This "unsafe" is also safe here:- V.unsafeFreeze v-+ final <- V.unsafeFreeze v+ finalGen <- readSTRef genVar+ pure (V.toList final, finalGen) -- | Picks from 0 to maxval, inclusive