Permalink
Browse files

Added Random instances for Int*, Word* and almost all types from Fore…

…ign.C.Types
  • Loading branch information...
1 parent 6c43f80 commit 94f0198568df8372f531dff057719d94233bef94 @basvandijk basvandijk committed Oct 8, 2010
Showing with 60 additions and 9 deletions.
  1. +60 −9 System/Random.hs
View
@@ -69,6 +69,8 @@ module System.Random
import Prelude
import Data.Int
+import Data.Word
+import Foreign.C.Types
#ifdef __NHC__
import CPUTime ( getCPUTime )
@@ -277,9 +279,41 @@ class Random a where
randomIO = getStdRandom random
-instance Random Int where
- randomR (a,b) g = randomIvalInteger (toInteger a, toInteger b) g
- random g = randomR (minBound,maxBound) g
+instance Random Integer where
+ randomR ival g = randomIvalInteger ival g
+ random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
+
+instance Random Int where randomR = randomIvalIntegral; random = randomBounded
+instance Random Int8 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Int16 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Int32 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Int64 where randomR = randomIvalIntegral; random = randomBounded
+
+instance Random Word where randomR = randomIvalIntegral; random = randomBounded
+instance Random Word8 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Word16 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Word32 where randomR = randomIvalIntegral; random = randomBounded
+instance Random Word64 where randomR = randomIvalIntegral; random = randomBounded
+
+instance Random CChar where randomR = randomIvalIntegral; random = randomBounded
+instance Random CSChar where randomR = randomIvalIntegral; random = randomBounded
+instance Random CUChar where randomR = randomIvalIntegral; random = randomBounded
+instance Random CShort where randomR = randomIvalIntegral; random = randomBounded
+instance Random CUShort where randomR = randomIvalIntegral; random = randomBounded
+instance Random CInt where randomR = randomIvalIntegral; random = randomBounded
+instance Random CUInt where randomR = randomIvalIntegral; random = randomBounded
+instance Random CLong where randomR = randomIvalIntegral; random = randomBounded
+instance Random CULong where randomR = randomIvalIntegral; random = randomBounded
+instance Random CPtrdiff where randomR = randomIvalIntegral; random = randomBounded
+instance Random CSize where randomR = randomIvalIntegral; random = randomBounded
+instance Random CWchar where randomR = randomIvalIntegral; random = randomBounded
+instance Random CSigAtomic where randomR = randomIvalIntegral; random = randomBounded
+instance Random CLLong where randomR = randomIvalIntegral; random = randomBounded
+instance Random CULLong where randomR = randomIvalIntegral; random = randomBounded
+instance Random CIntPtr where randomR = randomIvalIntegral; random = randomBounded
+instance Random CUIntPtr where randomR = randomIvalIntegral; random = randomBounded
+instance Random CIntMax where randomR = randomIvalIntegral; random = randomBounded
+instance Random CUIntMax where randomR = randomIvalIntegral; random = randomBounded
instance Random Char where
randomR (a,b) g =
@@ -301,26 +335,37 @@ instance Random Bool where
int2Bool _ = True
random g = randomR (minBound,maxBound) g
-
-instance Random Integer where
- randomR ival g = randomIvalInteger ival g
- random g = randomR (toInteger (minBound::Int), toInteger (maxBound::Int)) g
instance Random Double where
randomR ival g = randomIvalDouble ival id g
random g = randomR (0::Double,1) g
-- hah, so you thought you were saving cycles by using Float?
instance Random Float where
- random g = randomIvalDouble (0::Double,1) realToFrac g
- randomR (a,b) g = randomIvalDouble (realToFrac a, realToFrac b) realToFrac g
+ randomR = randomIvalFrac
+ random = randomFrac
+
+instance Random CFloat where
+ randomR = randomIvalFrac
+ random = randomFrac
+
+instance Random CDouble where
+ randomR = randomIvalFrac
+ random = randomFrac
+
mkStdRNG :: Integer -> IO StdGen
mkStdRNG o = do
ct <- getCPUTime
(sec, psec) <- getTime
return (createStdGen (sec * 12345 + psec + ct + o))
+randomBounded :: (RandomGen g, Random a, Bounded a) => g -> (a, g)
+randomBounded = randomR (minBound, maxBound)
+
+randomIvalIntegral :: (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
+randomIvalIntegral (l,h) = randomIvalInteger (toInteger l, toInteger h)
+
randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
randomIvalInteger (l,h) rng
| l > h = randomIvalInteger (h,l) rng
@@ -337,6 +382,12 @@ randomIvalInteger (l,h) rng
in
f (n' - 1) (fromIntegral x + acc * b) g'
+randomFrac :: (RandomGen g, Fractional a) => g -> (a, g)
+randomFrac = randomIvalDouble (0::Double,1) realToFrac
+
+randomIvalFrac :: (RandomGen g, Real a, Fractional b) => (a,a) -> g -> (b, g)
+randomIvalFrac (a,b) = randomIvalDouble (realToFrac a, realToFrac b) realToFrac
+
randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble (l,h) fromDouble rng
| l > h = randomIvalDouble (h,l) fromDouble rng

0 comments on commit 94f0198

Please sign in to comment.