#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
#include "MachDeps.h"
module System.Random
(
#ifdef ENABLE_SPLITTABLEGEN
RandomGen(next, genRange)
, SplittableGen(split)
#else
RandomGen(next, genRange, split)
#endif
, StdGen
, mkStdGen
, getStdRandom
, getStdGen
, setStdGen
, newStdGen
, Random ( random, randomR,
randoms, randomRs,
randomIO, randomRIO )
) where
import Prelude
import Data.Bits
import Data.Int
import Data.Word
import Foreign.C.Types
#ifdef __NHC__
import CPUTime ( getCPUTime )
import Foreign.Ptr ( Ptr, nullPtr )
import Foreign.C ( CTime, CUInt )
#else
import System.CPUTime ( getCPUTime )
import Data.Time ( getCurrentTime, UTCTime(..) )
import Data.Ratio ( numerator, denominator )
#endif
import Data.Char ( isSpace, chr, ord )
import System.IO.Unsafe ( unsafePerformIO )
import Data.IORef ( IORef, newIORef, readIORef, writeIORef )
#if MIN_VERSION_base (4,6,0)
import Data.IORef ( atomicModifyIORef' )
#else
import Data.IORef ( atomicModifyIORef )
#endif
import Numeric ( readDec )
#ifdef __GLASGOW_HASKELL__
import GHC.Exts ( build )
#else
{-# INLINE build #-}
build :: ((a -> [a] -> [a]) -> [a] -> [a]) -> [a]
build g = g (:) []
#endif
#if !MIN_VERSION_base (4,6,0)
atomicModifyIORef' :: IORef a -> (a -> (a,b)) -> IO b
atomicModifyIORef' ref f = do
b <- atomicModifyIORef ref
(\x -> let (a, b) = f x
in (a, a `seq` b))
b `seq` return b
#endif
#ifdef __NHC__
foreign import ccall "time.h time" readtime :: Ptr CTime -> IO CTime
getTime :: IO (Integer, Integer)
getTime = do CTime t <- readtime nullPtr; return (toInteger t, 0)
#else
getTime :: IO (Integer, Integer)
getTime :: IO (Integer, Integer)
getTime = do
UTCTime
utc <- IO UTCTime
getCurrentTime
let daytime :: Rational
daytime = DiffTime -> Rational
forall a. Real a => a -> Rational
toRational (DiffTime -> Rational) -> DiffTime -> Rational
forall a b. (a -> b) -> a -> b
$ UTCTime -> DiffTime
utctDayTime UTCTime
utc
(Integer, Integer) -> IO (Integer, Integer)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Integer, Integer) -> IO (Integer, Integer))
-> (Integer, Integer) -> IO (Integer, Integer)
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> (Integer, Integer)
forall a. Integral a => a -> a -> (a, a)
quotRem (Rational -> Integer
forall a. Ratio a -> a
numerator Rational
daytime) (Rational -> Integer
forall a. Ratio a -> a
denominator Rational
daytime)
#endif
#ifdef ENABLE_SPLITTABLEGEN
#else
#endif
class RandomGen g where
next :: g -> (Int, g)
genRange :: g -> (Int,Int)
genRange _ = (Int
forall a. Bounded a => a
minBound, Int
forall a. Bounded a => a
maxBound)
#ifdef ENABLE_SPLITTABLEGEN
class SplittableGen g where
#endif
split :: g -> (g, g)
data StdGen
= StdGen !Int32 !Int32
instance RandomGen StdGen where
next :: StdGen -> (Int, StdGen)
next = StdGen -> (Int, StdGen)
stdNext
genRange :: StdGen -> (Int, Int)
genRange _ = (Int, Int)
stdRange
#ifdef ENABLE_SPLITTABLEGEN
instance SplittableGen StdGen where
#endif
split :: StdGen -> (StdGen, StdGen)
split = StdGen -> (StdGen, StdGen)
stdSplit
instance Show StdGen where
showsPrec :: Int -> StdGen -> ShowS
showsPrec p :: Int
p (StdGen s1 :: Int32
s1 s2 :: Int32
s2) =
Int -> Int32 -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p Int32
s1 ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Char -> ShowS
showChar ' ' ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Int -> Int32 -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
p Int32
s2
instance Read StdGen where
readsPrec :: Int -> ReadS StdGen
readsPrec _p :: Int
_p = \ r :: String
r ->
case ReadS StdGen
try_read String
r of
r' :: [(StdGen, String)]
r'@[_] -> [(StdGen, String)]
r'
_ -> [String -> (StdGen, String)
stdFromString String
r]
where
try_read :: ReadS StdGen
try_read r :: String
r = do
(s1 :: Int32
s1, r1 :: String
r1) <- ReadS Int32
forall a. (Eq a, Num a) => ReadS a
readDec ((Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace String
r)
(s2 :: Int32
s2, r2 :: String
r2) <- ReadS Int32
forall a. (Eq a, Num a) => ReadS a
readDec ((Char -> Bool) -> ShowS
forall a. (a -> Bool) -> [a] -> [a]
dropWhile Char -> Bool
isSpace String
r1)
(StdGen, String) -> [(StdGen, String)]
forall (m :: * -> *) a. Monad m => a -> m a
return (Int32 -> Int32 -> StdGen
StdGen Int32
s1 Int32
s2, String
r2)
stdFromString :: String -> (StdGen, String)
stdFromString :: String -> (StdGen, String)
stdFromString s :: String
s = (Int -> StdGen
mkStdGen Int
num, String
rest)
where (cs :: String
cs, rest :: String
rest) = Int -> String -> (String, String)
forall a. Int -> [a] -> ([a], [a])
splitAt 6 String
s
num :: Int
num = (Int -> Int -> Int) -> Int -> [Int] -> Int
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\a :: Int
a x :: Int
x -> Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
a) 1 ((Char -> Int) -> String -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Char -> Int
ord String
cs)
mkStdGen :: Int -> StdGen
mkStdGen :: Int -> StdGen
mkStdGen s :: Int
s = Int32 -> StdGen
mkStdGen32 (Int32 -> StdGen) -> Int32 -> StdGen
forall a b. (a -> b) -> a -> b
$ Int -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
s
mkStdGen32 :: Int32 -> StdGen
mkStdGen32 :: Int32 -> StdGen
mkStdGen32 sMaybeNegative :: Int32
sMaybeNegative = Int32 -> Int32 -> StdGen
StdGen (Int32
s1Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+1) (Int32
s2Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+1)
where
s :: Int32
s = Int32
sMaybeNegative Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.&. Int32
forall a. Bounded a => a
maxBound
(q :: Int32
q, s1 :: Int32
s1) = Int32
s Int32 -> Int32 -> (Int32, Int32)
forall a. Integral a => a -> a -> (a, a)
`divMod` 2147483562
s2 :: Int32
s2 = Int32
q Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`mod` 2147483398
createStdGen :: Integer -> StdGen
createStdGen :: Integer -> StdGen
createStdGen s :: Integer
s = Int32 -> StdGen
mkStdGen32 (Int32 -> StdGen) -> Int32 -> StdGen
forall a b. (a -> b) -> a -> b
$ Integer -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
s
class Random a where
randomR :: RandomGen g => (a,a) -> g -> (a,g)
random :: RandomGen g => g -> (a, g)
{-# INLINE randomRs #-}
randomRs :: RandomGen g => (a,a) -> g -> [a]
randomRs ival :: (a, a)
ival g :: g
g = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\cons :: a -> b -> b
cons _nil :: b
_nil -> (a -> b -> b) -> (g -> (a, g)) -> g -> b
forall g a as.
RandomGen g =>
(a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms a -> b -> b
cons ((a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a, a)
ival) g
g)
{-# INLINE randoms #-}
randoms :: RandomGen g => g -> [a]
randoms g :: g
g = (forall b. (a -> b -> b) -> b -> b) -> [a]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
build (\cons :: a -> b -> b
cons _nil :: b
_nil -> (a -> b -> b) -> (g -> (a, g)) -> g -> b
forall g a as.
RandomGen g =>
(a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms a -> b -> b
cons g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g)
randomRIO :: (a,a) -> IO a
randomRIO range :: (a, a)
range = (StdGen -> (a, StdGen)) -> IO a
forall a. (StdGen -> (a, StdGen)) -> IO a
getStdRandom ((a, a) -> StdGen -> (a, StdGen)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a, a)
range)
randomIO :: IO a
randomIO = (StdGen -> (a, StdGen)) -> IO a
forall a. (StdGen -> (a, StdGen)) -> IO a
getStdRandom StdGen -> (a, StdGen)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random
{-# INLINE buildRandoms #-}
buildRandoms :: RandomGen g
=> (a -> as -> as)
-> (g -> (a,g))
-> g
-> as
buildRandoms :: (a -> as -> as) -> (g -> (a, g)) -> g -> as
buildRandoms cons :: a -> as -> as
cons rand :: g -> (a, g)
rand = g -> as
go
where
go :: g -> as
go g :: g
g = a
x a -> as -> as
forall a b. a -> b -> b
`seq` (a
x a -> as -> as
`cons` g -> as
go g
g') where (x :: a
x,g' :: g
g') = g -> (a, g)
rand g
g
instance Random Integer where
randomR :: (Integer, Integer) -> g -> (Integer, g)
randomR ival :: (Integer, Integer)
ival g :: g
g = (Integer, Integer) -> g -> (Integer, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (Integer, Integer)
ival g
g
random :: g -> (Integer, g)
random g :: g
g = (Integer, Integer) -> g -> (Integer, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
minBound::Int), Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound::Int)) g
g
instance Random Int where randomR :: (Int, Int) -> g -> (Int, g)
randomR = (Int, Int) -> g -> (Int, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Int, g)
random = g -> (Int, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Int8 where randomR :: (Int8, Int8) -> g -> (Int8, g)
randomR = (Int8, Int8) -> g -> (Int8, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Int8, g)
random = g -> (Int8, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Int16 where randomR :: (Int16, Int16) -> g -> (Int16, g)
randomR = (Int16, Int16) -> g -> (Int16, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Int16, g)
random = g -> (Int16, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Int32 where randomR :: (Int32, Int32) -> g -> (Int32, g)
randomR = (Int32, Int32) -> g -> (Int32, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Int32, g)
random = g -> (Int32, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Int64 where randomR :: (Int64, Int64) -> g -> (Int64, g)
randomR = (Int64, Int64) -> g -> (Int64, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Int64, g)
random = g -> (Int64, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
#ifndef __NHC__
instance Random Word where randomR :: (Word, Word) -> g -> (Word, g)
randomR = (Word, Word) -> g -> (Word, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Word, g)
random = g -> (Word, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
#endif
instance Random Word8 where randomR :: (Word8, Word8) -> g -> (Word8, g)
randomR = (Word8, Word8) -> g -> (Word8, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Word8, g)
random = g -> (Word8, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Word16 where randomR :: (Word16, Word16) -> g -> (Word16, g)
randomR = (Word16, Word16) -> g -> (Word16, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Word16, g)
random = g -> (Word16, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Word32 where randomR :: (Word32, Word32) -> g -> (Word32, g)
randomR = (Word32, Word32) -> g -> (Word32, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Word32, g)
random = g -> (Word32, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Word64 where randomR :: (Word64, Word64) -> g -> (Word64, g)
randomR = (Word64, Word64) -> g -> (Word64, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (Word64, g)
random = g -> (Word64, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CChar where randomR :: (CChar, CChar) -> g -> (CChar, g)
randomR = (CChar, CChar) -> g -> (CChar, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CChar, g)
random = g -> (CChar, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CSChar where randomR :: (CSChar, CSChar) -> g -> (CSChar, g)
randomR = (CSChar, CSChar) -> g -> (CSChar, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CSChar, g)
random = g -> (CSChar, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CUChar where randomR :: (CUChar, CUChar) -> g -> (CUChar, g)
randomR = (CUChar, CUChar) -> g -> (CUChar, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CUChar, g)
random = g -> (CUChar, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CShort where randomR :: (CShort, CShort) -> g -> (CShort, g)
randomR = (CShort, CShort) -> g -> (CShort, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CShort, g)
random = g -> (CShort, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CUShort where randomR :: (CUShort, CUShort) -> g -> (CUShort, g)
randomR = (CUShort, CUShort) -> g -> (CUShort, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CUShort, g)
random = g -> (CUShort, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CInt where randomR :: (CInt, CInt) -> g -> (CInt, g)
randomR = (CInt, CInt) -> g -> (CInt, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CInt, g)
random = g -> (CInt, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CUInt where randomR :: (CUInt, CUInt) -> g -> (CUInt, g)
randomR = (CUInt, CUInt) -> g -> (CUInt, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CUInt, g)
random = g -> (CUInt, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CLong where randomR :: (CLong, CLong) -> g -> (CLong, g)
randomR = (CLong, CLong) -> g -> (CLong, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CLong, g)
random = g -> (CLong, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CULong where randomR :: (CULong, CULong) -> g -> (CULong, g)
randomR = (CULong, CULong) -> g -> (CULong, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CULong, g)
random = g -> (CULong, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CPtrdiff where randomR :: (CPtrdiff, CPtrdiff) -> g -> (CPtrdiff, g)
randomR = (CPtrdiff, CPtrdiff) -> g -> (CPtrdiff, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CPtrdiff, g)
random = g -> (CPtrdiff, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CSize where randomR :: (CSize, CSize) -> g -> (CSize, g)
randomR = (CSize, CSize) -> g -> (CSize, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CSize, g)
random = g -> (CSize, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CWchar where randomR :: (CWchar, CWchar) -> g -> (CWchar, g)
randomR = (CWchar, CWchar) -> g -> (CWchar, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CWchar, g)
random = g -> (CWchar, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CSigAtomic where randomR :: (CSigAtomic, CSigAtomic) -> g -> (CSigAtomic, g)
randomR = (CSigAtomic, CSigAtomic) -> g -> (CSigAtomic, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CSigAtomic, g)
random = g -> (CSigAtomic, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CLLong where randomR :: (CLLong, CLLong) -> g -> (CLLong, g)
randomR = (CLLong, CLLong) -> g -> (CLLong, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CLLong, g)
random = g -> (CLLong, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CULLong where randomR :: (CULLong, CULLong) -> g -> (CULLong, g)
randomR = (CULLong, CULLong) -> g -> (CULLong, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CULLong, g)
random = g -> (CULLong, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CIntPtr where randomR :: (CIntPtr, CIntPtr) -> g -> (CIntPtr, g)
randomR = (CIntPtr, CIntPtr) -> g -> (CIntPtr, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CIntPtr, g)
random = g -> (CIntPtr, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CUIntPtr where randomR :: (CUIntPtr, CUIntPtr) -> g -> (CUIntPtr, g)
randomR = (CUIntPtr, CUIntPtr) -> g -> (CUIntPtr, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CUIntPtr, g)
random = g -> (CUIntPtr, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CIntMax where randomR :: (CIntMax, CIntMax) -> g -> (CIntMax, g)
randomR = (CIntMax, CIntMax) -> g -> (CIntMax, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CIntMax, g)
random = g -> (CIntMax, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random CUIntMax where randomR :: (CUIntMax, CUIntMax) -> g -> (CUIntMax, g)
randomR = (CUIntMax, CUIntMax) -> g -> (CUIntMax, g)
forall g a. (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral; random :: g -> (CUIntMax, g)
random = g -> (CUIntMax, g)
forall g a. (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded
instance Random Char where
randomR :: (Char, Char) -> g -> (Char, g)
randomR (a :: Char
a,b :: Char
b) g :: g
g =
case ((Integer, Integer) -> g -> (Int, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Char -> Int
ord Char
a), Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Char -> Int
ord Char
b)) g
g) of
(x :: Int
x,g' :: g
g') -> (Int -> Char
chr Int
x, g
g')
random :: g -> (Char, g)
random g :: g
g = (Char, Char) -> g -> (Char, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (Char
forall a. Bounded a => a
minBound,Char
forall a. Bounded a => a
maxBound) g
g
instance Random Bool where
randomR :: (Bool, Bool) -> g -> (Bool, g)
randomR (a :: Bool
a,b :: Bool
b) g :: g
g =
case ((Integer, Integer) -> g -> (Int, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (Bool -> Integer
bool2Int Bool
a, Bool -> Integer
bool2Int Bool
b) g
g) of
(x :: Int
x, g' :: g
g') -> (Int -> Bool
int2Bool Int
x, g
g')
where
bool2Int :: Bool -> Integer
bool2Int :: Bool -> Integer
bool2Int False = 0
bool2Int True = 1
int2Bool :: Int -> Bool
int2Bool :: Int -> Bool
int2Bool 0 = Bool
False
int2Bool _ = Bool
True
random :: g -> (Bool, g)
random g :: g
g = (Bool, Bool) -> g -> (Bool, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (Bool
forall a. Bounded a => a
minBound,Bool
forall a. Bounded a => a
maxBound) g
g
{-# INLINE randomRFloating #-}
randomRFloating :: (Fractional a, Num a, Ord a, Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomRFloating :: (a, a) -> g -> (a, g)
randomRFloating (l :: a
l,h :: a
h) g :: g
g
| a
la -> a -> Bool
forall a. Ord a => a -> a -> Bool
>a
h = (a, a) -> g -> (a, g)
forall a g.
(Fractional a, Num a, Ord a, Random a, RandomGen g) =>
(a, a) -> g -> (a, g)
randomRFloating (a
h,a
l) g
g
| Bool
otherwise = let (coef :: a
coef,g' :: g
g') = g -> (a, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
g in
(2.0 a -> a -> a
forall a. Num a => a -> a -> a
* (0.5a -> a -> a
forall a. Num a => a -> a -> a
*a
l a -> a -> a
forall a. Num a => a -> a -> a
+ a
coef a -> a -> a
forall a. Num a => a -> a -> a
* (0.5a -> a -> a
forall a. Num a => a -> a -> a
*a
h a -> a -> a
forall a. Num a => a -> a -> a
- 0.5a -> a -> a
forall a. Num a => a -> a -> a
*a
l)), g
g')
instance Random Double where
randomR :: (Double, Double) -> g -> (Double, g)
randomR = (Double, Double) -> g -> (Double, g)
forall a g.
(Fractional a, Num a, Ord a, Random a, RandomGen g) =>
(a, a) -> g -> (a, g)
randomRFloating
random :: g -> (Double, g)
random rng :: g
rng =
case g -> (Int64, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
rng of
(x :: Int64
x,rng' :: g
rng') ->
((Int64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int64
mask53 Int64 -> Int64 -> Int64
forall a. Bits a => a -> a -> a
.&. (Int64
x::Int64)) :: Double)
Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int64 -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
twoto53, g
rng')
where
twoto53 :: Int64
twoto53 = (2::Int64) Int64 -> Int64 -> Int64
forall a b. (Num a, Integral b) => a -> b -> a
^ (53::Int64)
mask53 :: Int64
mask53 = Int64
twoto53 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
- 1
instance Random Float where
randomR :: (Float, Float) -> g -> (Float, g)
randomR = (Float, Float) -> g -> (Float, g)
forall a g.
(Fractional a, Num a, Ord a, Random a, RandomGen g) =>
(a, a) -> g -> (a, g)
randomRFloating
random :: g -> (Float, g)
random rng :: g
rng =
case g -> (Int32, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
rng of
(x :: Int32
x,rng' :: g
rng') ->
((Int32 -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32
mask24 Int32 -> Int32 -> Int32
forall a. Bits a => a -> a -> a
.&. (Int32
x::Int32)) :: Float)
Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Int32 -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
twoto24, g
rng')
where
mask24 :: Int32
mask24 = Int32
twoto24 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- 1
twoto24 :: Int32
twoto24 = (2::Int32) Int32 -> Int32 -> Int32
forall a b. (Num a, Integral b) => a -> b -> a
^ (24::Int32)
instance Random CFloat where
randomR :: (CFloat, CFloat) -> g -> (CFloat, g)
randomR = (CFloat, CFloat) -> g -> (CFloat, g)
forall a g.
(Fractional a, Num a, Ord a, Random a, RandomGen g) =>
(a, a) -> g -> (a, g)
randomRFloating
random :: g -> (CFloat, g)
random rng :: g
rng = case g -> (Float, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
rng of
(x :: Float
x,rng' :: g
rng') -> (Float -> CFloat
forall a b. (Real a, Fractional b) => a -> b
realToFrac (Float
x::Float), g
rng')
instance Random CDouble where
randomR :: (CDouble, CDouble) -> g -> (CDouble, g)
randomR = (CDouble, CDouble) -> g -> (CDouble, g)
forall a g.
(Fractional a, Num a, Ord a, Random a, RandomGen g) =>
(a, a) -> g -> (a, g)
randomRFloating
random :: g -> (CDouble, g)
random = g -> (CDouble, g)
forall g a. (RandomGen g, Fractional a) => g -> (a, g)
randomFrac
mkStdRNG :: Integer -> IO StdGen
mkStdRNG :: Integer -> IO StdGen
mkStdRNG o :: Integer
o = do
Integer
ct <- IO Integer
getCPUTime
(sec :: Integer
sec, psec :: Integer
psec) <- IO (Integer, Integer)
getTime
StdGen -> IO StdGen
forall (m :: * -> *) a. Monad m => a -> m a
return (Integer -> StdGen
createStdGen (Integer
sec Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* 12345 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
psec Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
ct Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
o))
randomBounded :: (RandomGen g, Random a, Bounded a) => g -> (a, g)
randomBounded :: g -> (a, g)
randomBounded = (a, a) -> g -> (a, g)
forall a g. (Random a, RandomGen g) => (a, a) -> g -> (a, g)
randomR (a
forall a. Bounded a => a
minBound, a
forall a. Bounded a => a
maxBound)
randomIvalIntegral :: (RandomGen g, Integral a) => (a, a) -> g -> (a, g)
randomIvalIntegral :: (a, a) -> g -> (a, g)
randomIvalIntegral (l :: a
l,h :: a
h) = (Integer, Integer) -> g -> (a, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (a -> Integer
forall a. Integral a => a -> Integer
toInteger a
l, a -> Integer
forall a. Integral a => a -> Integer
toInteger a
h)
{-# SPECIALIZE randomIvalInteger :: (Num a) =>
(Integer, Integer) -> StdGen -> (a, StdGen) #-}
randomIvalInteger :: (RandomGen g, Num a) => (Integer, Integer) -> g -> (a, g)
randomIvalInteger :: (Integer, Integer) -> g -> (a, g)
randomIvalInteger (l :: Integer
l,h :: Integer
h) rng :: g
rng
| Integer
l Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
h = (Integer, Integer) -> g -> (a, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (Integer
h,Integer
l) g
rng
| Bool
otherwise = case (Integer -> Integer -> g -> (Integer, g)
forall t. RandomGen t => Integer -> Integer -> t -> (Integer, t)
f 1 0 g
rng) of (v :: Integer
v, rng' :: g
rng') -> (Integer -> a
forall a. Num a => Integer -> a
fromInteger (Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Integer
v Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
k), g
rng')
where
(genlo :: Int
genlo, genhi :: Int
genhi) = g -> (Int, Int)
forall g. RandomGen g => g -> (Int, Int)
genRange g
rng
b :: Integer
b = Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
genhi Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
genlo Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 1
q :: Integer
q = 1000
k :: Integer
k = Integer
h Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
l Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 1
magtgt :: Integer
magtgt = Integer
k Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
q
f :: Integer -> Integer -> t -> (Integer, t)
f mag :: Integer
mag v :: Integer
v g :: t
g | Integer
mag Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
magtgt = (Integer
v, t
g)
| Bool
otherwise = Integer
v' Integer -> (Integer, t) -> (Integer, t)
forall a b. a -> b -> b
`seq`Integer -> Integer -> t -> (Integer, t)
f (Integer
magInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
*Integer
b) Integer
v' t
g' where
(x :: Int
x,g' :: t
g') = t -> (Int, t)
forall g. RandomGen g => g -> (Int, g)
next t
g
v' :: Integer
v' = (Integer
v Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
b Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
genlo))
randomFrac :: (RandomGen g, Fractional a) => g -> (a, g)
randomFrac :: g -> (a, g)
randomFrac = (Double, Double) -> (Double -> a) -> g -> (a, g)
forall g a.
(RandomGen g, Fractional a) =>
(Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble (0::Double,1) Double -> a
forall a b. (Real a, Fractional b) => a -> b
realToFrac
randomIvalDouble :: (RandomGen g, Fractional a) => (Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble :: (Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble (l :: Double
l,h :: Double
h) fromDouble :: Double -> a
fromDouble rng :: g
rng
| Double
l Double -> Double -> Bool
forall a. Ord a => a -> a -> Bool
> Double
h = (Double, Double) -> (Double -> a) -> g -> (a, g)
forall g a.
(RandomGen g, Fractional a) =>
(Double, Double) -> (Double -> a) -> g -> (a, g)
randomIvalDouble (Double
h,Double
l) Double -> a
fromDouble g
rng
| Bool
otherwise =
case ((Integer, Integer) -> g -> (Int32, g)
forall g a.
(RandomGen g, Num a) =>
(Integer, Integer) -> g -> (a, g)
randomIvalInteger (Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
minBound::Int32), Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
maxBound::Int32)) g
rng) of
(x :: Int32
x, rng' :: g
rng') ->
let
scaled_x :: a
scaled_x =
Double -> a
fromDouble (0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
l Double -> Double -> Double
forall a. Num a => a -> a -> a
+ 0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
h) a -> a -> a
forall a. Num a => a -> a -> a
+
Double -> a
fromDouble ((0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
h Double -> Double -> Double
forall a. Num a => a -> a -> a
- 0.5Double -> Double -> Double
forall a. Num a => a -> a -> a
*Double
l) Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ (0.5 Double -> Double -> Double
forall a. Num a => a -> a -> a
* Integer -> Double
forall a b. (Real a, Fractional b) => a -> b
realToFrac Integer
int32Count)) a -> a -> a
forall a. Num a => a -> a -> a
*
Int32 -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32
x::Int32)
in
(a
scaled_x, g
rng')
int32Count :: Integer
int32Count :: Integer
int32Count = Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
maxBound::Int32) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Int32 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int32
forall a. Bounded a => a
minBound::Int32) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ 1
stdRange :: (Int,Int)
stdRange :: (Int, Int)
stdRange = (1, 2147483562)
stdNext :: StdGen -> (Int, StdGen)
stdNext :: StdGen -> (Int, StdGen)
stdNext (StdGen s1 :: Int32
s1 s2 :: Int32
s2) = (Int32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int32
z', Int32 -> Int32 -> StdGen
StdGen Int32
s1'' Int32
s2'')
where z' :: Int32
z' = if Int32
z Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< 1 then Int32
z Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ 2147483562 else Int32
z
z :: Int32
z = Int32
s1'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
s2''
k :: Int32
k = Int32
s1 Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` 53668
s1' :: Int32
s1' = 40014 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* (Int32
s1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
k Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* 53668) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
k Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* 12211
s1'' :: Int32
s1'' = if Int32
s1' Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< 0 then Int32
s1' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ 2147483563 else Int32
s1'
k' :: Int32
k' = Int32
s2 Int32 -> Int32 -> Int32
forall a. Integral a => a -> a -> a
`quot` 52774
s2' :: Int32
s2' = 40692 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* (Int32
s2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
k' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* 52774) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
k' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* 3791
s2'' :: Int32
s2'' = if Int32
s2' Int32 -> Int32 -> Bool
forall a. Ord a => a -> a -> Bool
< 0 then Int32
s2' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ 2147483399 else Int32
s2'
stdSplit :: StdGen -> (StdGen, StdGen)
stdSplit :: StdGen -> (StdGen, StdGen)
stdSplit std :: StdGen
std@(StdGen s1 :: Int32
s1 s2 :: Int32
s2)
= (StdGen
left, StdGen
right)
where
left :: StdGen
left = Int32 -> Int32 -> StdGen
StdGen Int32
new_s1 Int32
t2
right :: StdGen
right = Int32 -> Int32 -> StdGen
StdGen Int32
t1 Int32
new_s2
new_s1 :: Int32
new_s1 | Int32
s1 Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== 2147483562 = 1
| Bool
otherwise = Int32
s1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ 1
new_s2 :: Int32
new_s2 | Int32
s2 Int32 -> Int32 -> Bool
forall a. Eq a => a -> a -> Bool
== 1 = 2147483398
| Bool
otherwise = Int32
s2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- 1
StdGen t1 :: Int32
t1 t2 :: Int32
t2 = (Int, StdGen) -> StdGen
forall a b. (a, b) -> b
snd (StdGen -> (Int, StdGen)
forall g. RandomGen g => g -> (Int, g)
next StdGen
std)
setStdGen :: StdGen -> IO ()
setStdGen :: StdGen -> IO ()
setStdGen sgen :: StdGen
sgen = IORef StdGen -> StdGen -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef StdGen
theStdGen StdGen
sgen
getStdGen :: IO StdGen
getStdGen :: IO StdGen
getStdGen = IORef StdGen -> IO StdGen
forall a. IORef a -> IO a
readIORef IORef StdGen
theStdGen
theStdGen :: IORef StdGen
theStdGen :: IORef StdGen
theStdGen = IO (IORef StdGen) -> IORef StdGen
forall a. IO a -> a
unsafePerformIO (IO (IORef StdGen) -> IORef StdGen)
-> IO (IORef StdGen) -> IORef StdGen
forall a b. (a -> b) -> a -> b
$ do
StdGen
rng <- Integer -> IO StdGen
mkStdRNG 0
StdGen -> IO (IORef StdGen)
forall a. a -> IO (IORef a)
newIORef StdGen
rng
newStdGen :: IO StdGen
newStdGen :: IO StdGen
newStdGen = IORef StdGen -> (StdGen -> (StdGen, StdGen)) -> IO StdGen
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef StdGen
theStdGen StdGen -> (StdGen, StdGen)
forall g. RandomGen g => g -> (g, g)
split
getStdRandom :: (StdGen -> (a,StdGen)) -> IO a
getStdRandom :: (StdGen -> (a, StdGen)) -> IO a
getStdRandom f :: StdGen -> (a, StdGen)
f = IORef StdGen -> (StdGen -> (StdGen, a)) -> IO a
forall a b. IORef a -> (a -> (a, b)) -> IO b
atomicModifyIORef' IORef StdGen
theStdGen ((a, StdGen) -> (StdGen, a)
forall b a. (b, a) -> (a, b)
swap ((a, StdGen) -> (StdGen, a))
-> (StdGen -> (a, StdGen)) -> StdGen -> (StdGen, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. StdGen -> (a, StdGen)
f)
where swap :: (b, a) -> (a, b)
swap (v :: b
v,g :: a
g) = (a
g,b
v)