tf-random 0.2 → 0.3
raw patch · 5 files changed
+82/−5 lines, 5 files
Files
- ChangeLog +9/−0
- src/System/Random/TF.hs +1/−1
- src/System/Random/TF/Init.hs +41/−2
- src/System/Random/TF/Instances.hs +29/−1
- tf-random.cabal +2/−1
+ ChangeLog view
@@ -0,0 +1,9 @@+0.3+ * Added newTFGen and mkTFGen.+ * Small additions to Intances.++0.2+ * Compatibility with older base up to 4.2++0.1+ * Initial release
src/System/Random/TF.hs view
@@ -20,5 +20,5 @@ where import System.Random.TF.Gen hiding (RandomGen)-import System.Random.TF.Init+import System.Random.TF.Init hiding (initTFGen)
src/System/Random/TF/Init.hs view
@@ -1,3 +1,4 @@+{-# LANGUAGE CPP #-} -- Module : System.Random.TF.Init -- Copyright : (c) 2013 Michał Pałka -- License : BSD3@@ -7,19 +8,24 @@ -- Portability : portable -- module System.Random.TF.Init+ (newTFGen, mkTFGen, mkSeedTime, mkSeedUnix, initTFGen) where +import System.Random.TF.Gen (TFGen, seedTFGen, split)+ import Control.Monad (when) ---import Data.Int+import Data.Bits (bitSize)+import Data.IORef import Data.Word -import Foreign+import Foreign (allocaBytes, peekArray) import Data.Ratio (numerator, denominator) import Data.Time import System.CPUTime import System.IO+import System.IO.Unsafe (unsafePerformIO) -- | Use system time create the random seed. -- This method of seeding may not be relible.@@ -54,3 +60,36 @@ let [x1, x2, x3, x4] = l return (x1, x2, x3, x4) +-- | Create a seed and used it to seed an instance of TFGen.+-- Uses 'mkSeedUnix' on UNIX, and 'mkSeedTime' otherwise.+initTFGen :: IO TFGen+initTFGen = do+#ifdef UNIX+ s <- mkSeedUnix+#else+ s <- mkSeedTime+#endif+ return $ seedTFGen s++-- | Derive a new generator instance from the global RNG using split.+-- This is the default way of obtaining a new RNG instance.+-- Initial generator is seeded using 'mkSeedUnix' on UNIX,+-- and 'mkSeedTime' otherwise. This should be eventually+-- replaced with proper seeding.++-- Inspired by System.Random+newTFGen :: IO TFGen+newTFGen = atomicModifyIORef theTFGen split++{-# NOINLINE theTFGen #-}+theTFGen :: IORef TFGen+theTFGen = unsafePerformIO $ do+ rng <- initTFGen+ newIORef rng++-- | Quick and dirty way of creating a deterministically+-- seeded generator.+mkTFGen :: Int -> TFGen+mkTFGen n+ | bitSize n > 64 = error "mkTFGen: case where size of Int > 64 not implemented"+ | otherwise = seedTFGen (fromIntegral n, 0, 0, 0)
src/System/Random/TF/Instances.hs view
@@ -13,7 +13,7 @@ -- the 'System.Random.TF.Gen.RandomGen' class from "System.Random.TF.Gen". module System.Random.TF.Instances- (Random (..)) where+ (Random (..), randomEnum) where import Data.Bits import Data.Int@@ -237,3 +237,31 @@ instance Random Int64 where randomR = randomInt64 random = randomBounded++instance Random Word8 where+ randomR (l, h) g =+ let (x, g') = randomWord32 (fromIntegral l, fromIntegral h) g+ in (fromIntegral x, g')+ -- Optimised version+ random g = let (x, g') = next g in (fromIntegral x, g')++instance Random Int8 where+ randomR (l, h) g =+ let (x, g') = randomInt32 (fromIntegral l, fromIntegral h) g+ in (fromIntegral x, g')+ -- Optimised version+ random g = let (x, g') = next g in (fromIntegral x, g')++instance Random Word16 where+ randomR (l, h) g =+ let (x, g') = randomWord32 (fromIntegral l, fromIntegral h) g+ in (fromIntegral x, g')+ -- Optimised version+ random g = let (x, g') = next g in (fromIntegral x, g')++instance Random Int16 where+ randomR (l, h) g =+ let (x, g') = randomInt32 (fromIntegral l, fromIntegral h) g+ in (fromIntegral x, g')+ -- Optimised version+ random g = let (x, g') = next g in (fromIntegral x, g')
tf-random.cabal view
@@ -10,7 +10,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.2+version: 0.3 -- A short (one-line) description of the package. synopsis: High-quality splittable pseudorandom number generator@@ -59,6 +59,7 @@ cabal-version: >=1.8 extra-source-files:+ ChangeLog, LICENSE.brg, LICENSE.tf, cbits/brg_types.h,