#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif

-----------------------------------------------------------------------------
-- |
-- Module      :  System.Random
-- Copyright   :  (c) The University of Glasgow 2001
-- License     :  BSD-style (see the file LICENSE in the 'random' repository)
-- 
-- Maintainer  :  libraries@haskell.org
-- Stability   :  stable
-- Portability :  portable
--
-- This library deals with the common task of pseudo-random number
-- generation. The library makes it possible to generate repeatable
-- results, by starting with a specified initial random number generator,
-- or to get different results on each run by using the system-initialised
-- generator or by supplying a seed from some other source.
--
-- The library is split into two layers: 
--
-- * A core /random number generator/ provides a supply of bits.
--   The class 'RandomGen' provides a common interface to such generators.
--   The library provides one instance of 'RandomGen', the abstract
--   data type 'StdGen'.  Programmers may, of course, supply their own
--   instances of 'RandomGen'.
--
-- * The class 'Random' provides a way to extract values of a particular
--   type from a random number generator.  For example, the 'Float'
--   instance of 'Random' allows one to generate random values of type
--   'Float'.
--
-- This implementation uses the Portable Combined Generator of L'Ecuyer
-- ["System.Random\#LEcuyer"] for 32-bit computers, transliterated by
-- Lennart Augustsson.  It has a period of roughly 2.30584e18.
--
-----------------------------------------------------------------------------

#include "MachDeps.h"

module System.Random
	(

	-- $intro

	-- * Random number generators

#ifdef ENABLE_SPLITTABLEGEN
	  RandomGen(next, genRange)
	, SplittableGen(split)
#else
	  RandomGen(next, genRange, split)
#endif
	-- ** Standard random number generators
	, StdGen
	, mkStdGen

	-- ** The global random number generator

	-- $globalrng

	, getStdRandom
	, getStdGen
	, setStdGen
	, newStdGen

	-- * Random values of various types
	, Random ( random,   randomR,
		   randoms,  randomRs,
		   randomIO, randomRIO )

	-- * References
	-- $references

	) 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
-- | A dummy variant of build without fusion.
{-# 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

-- The standard nhc98 implementation of Time.ClockTime does not match
-- the extended one expected in this module, so we lash-up a quick
-- replacement here.
#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

-- | The class 'RandomGen' provides a common interface to random number
-- generators.
--
#ifdef ENABLE_SPLITTABLEGEN
-- Minimal complete definition: 'next'.
#else
-- Minimal complete definition: 'next' and 'split'.
#endif

class RandomGen g where

   -- |The 'next' operation returns an 'Int' that is uniformly distributed
   -- in the range returned by 'genRange' (including both end points),
   -- and a new generator.
   next     :: g -> (Int, g)

   -- |The 'genRange' operation yields the range of values returned by
   -- the generator.
   --
   -- It is required that:
   --
   -- * If @(a,b) = 'genRange' g@, then @a < b@.
   --
   -- * 'genRange' always returns a pair of defined 'Int's.
   --
   -- The second condition ensures that 'genRange' cannot examine its
   -- argument, and hence the value it returns can be determined only by the
   -- instance of 'RandomGen'.  That in turn allows an implementation to make
   -- a single call to 'genRange' to establish a generator's range, without
   -- being concerned that the generator returned by (say) 'next' might have
   -- a different range to the generator passed to 'next'.
   --
   -- The default definition spans the full range of 'Int'.
   genRange :: g -> (Int,Int)

   -- default method
   genRange _ = (Int
forall a. Bounded a => a
minBound, Int
forall a. Bounded a => a
maxBound)

#ifdef ENABLE_SPLITTABLEGEN
-- | The class 'SplittableGen' proivides a way to specify a random number
--   generator that can be split into two new generators.
class SplittableGen g where
#endif
   -- |The 'split' operation allows one to obtain two distinct random number
   -- generators. This is very useful in functional programs (for example, when
   -- passing a random number generator down to recursive calls), but very
   -- little work has been done on statistically robust implementations of
   -- 'split' (["System.Random\#Burton", "System.Random\#Hellekalek"]
   -- are the only examples we know of).
   split    :: g -> (g, g)

{- |
The 'StdGen' instance of 'RandomGen' has a 'genRange' of at least 30 bits.

The result of repeatedly using 'next' should be at least as statistically
robust as the /Minimal Standard Random Number Generator/ described by
["System.Random\#Park", "System.Random\#Carta"].
Until more is known about implementations of 'split', all we require is
that 'split' deliver generators that are (a) not identical and
(b) independently robust in the sense just given.

The 'Show' and 'Read' instances of 'StdGen' provide a primitive way to save the
state of a random number generator.
It is required that @'read' ('show' g) == g@.

In addition, 'reads' may be used to map an arbitrary string (not necessarily one
produced by 'show') onto a value of type 'StdGen'. In general, the 'Read'
instance of 'StdGen' has the following properties: 

* It guarantees to succeed on any string. 

* It guarantees to consume only a finite portion of the string. 

* Different argument strings are likely to result in different results.

-}

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] -- because it shouldn't ever fail.
    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)

{-
 If we cannot unravel the StdGen from a string, create
 one based on the string given.
-}
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)


{- |
The function 'mkStdGen' provides an alternative way of producing an initial
generator, by mapping an 'Int' into a generator. Again, distinct arguments
should be likely to produce distinct generators.
-}
mkStdGen :: Int -> StdGen -- why not Integer ?
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

{-
From ["System.Random\#LEcuyer"]: "The integer variables s1 and s2 ... must be
initialized to values in the range [1, 2147483562] and [1, 2147483398]
respectively."
-}
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
	-- We want a non-negative number, but we can't just take the abs
	-- of sMaybeNegative as -minBound == minBound.
	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

{- |
With a source of random number supply in hand, the 'Random' class allows the
programmer to extract random values of a variety of types.

Minimal complete definition: 'randomR' and 'random'.

-}

class Random a where
  -- | Takes a range /(lo,hi)/ and a random number generator
  -- /g/, and returns a random value uniformly distributed in the closed
  -- interval /[lo,hi]/, together with a new generator. It is unspecified
  -- what happens if /lo>hi/. For continuous types there is no requirement
  -- that the values /lo/ and /hi/ are ever produced, but they may be,
  -- depending on the implementation and the interval.
  randomR :: RandomGen g => (a,a) -> g -> (a,g)

  -- | The same as 'randomR', but using a default range determined by the type:
  --
  -- * For bounded types (instances of 'Bounded', such as 'Char'),
  --   the range is normally the whole type.
  --
  -- * For fractional types, the range is normally the semi-closed interval
  -- @[0,1)@.
  --
  -- * For 'Integer', the range is (arbitrarily) the range of 'Int'.
  random  :: RandomGen g => g -> (a, g)

  -- | Plural variant of 'randomR', producing an infinite list of
  -- random values instead of returning a new generator.
  {-# 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)

  -- | Plural variant of 'random', producing an infinite list of
  -- random values instead of returning a new generator.
  {-# 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)

  -- | A variant of 'randomR' that uses the global random number generator
  -- (see "System.Random#globalrng").
  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)

  -- | A variant of 'random' that uses the global random number generator
  -- (see "System.Random#globalrng").
  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

-- | Produce an infinite list-equivalent of random values.
{-# INLINE buildRandoms #-}
buildRandoms :: RandomGen g
             => (a -> as -> as)  -- ^ E.g. '(:)' but subject to fusion
             -> (g -> (a,g))     -- ^ E.g. 'random'
             -> g                -- ^ A 'RandomGen' instance
             -> 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
    -- The seq fixes part of #4218 and also makes fused Core simpler.
    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__
-- Word is a type synonym in nhc98.
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')  -- avoid overflow

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') -> 
          -- We use 53 bits of randomness corresponding to the 53 bit significand:
          ((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 = 
    -- TODO: Faster to just use 'next' IF it generates enough bits of randomness.   
    case g -> (Int32, g)
forall a g. (Random a, RandomGen g) => g -> (a, g)
random g
rng of 
      (x :: Int32
x,rng' :: g
rng') -> 
          -- We use 24 bits of randomness corresponding to the 24 bit significand:
          ((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')
	 -- Note, encodeFloat is another option, but I'm not seeing slightly
	 --  worse performance with the following [2011.06.25]:
--         (encodeFloat rand (-24), 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)

-- CFloat/CDouble are basically the same as a Float/Double:
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
  -- A MYSTERY:
  -- Presently, this is showing better performance than the Double instance:
  -- (And yet, if the Double instance uses randomFrac then its performance is much worse!)
  random :: g -> (CDouble, g)
random  = g -> (CDouble, g)
forall g a. (RandomGen g, Fractional a) => g -> (a, g)
randomFrac
  -- random rng = case random rng of 
  -- 	         (x,rng') -> (realToFrac (x::Double), rng')

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)

-- The two integer functions below take an [inclusive,inclusive] range.
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

       -- Probabilities of the most likely and least likely result
       -- will differ at most by a factor of (1 +- 1/q).  Assuming the RandomGen
       -- is uniform, of course

       -- On average, log q / log b more random values will be generated
       -- than the minimum
       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

       -- generate random values until we exceed the target magnitude 
       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))


-- The continuous functions on the other hand take an [inclusive,exclusive) range.
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
+                   -- previously (l+h)/2, overflowed
                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
*  -- avoid overflow
		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  -- GHC ticket #3982

stdRange :: (Int,Int)
stdRange :: (Int, Int)
stdRange = (1, 2147483562)

stdNext :: StdGen -> (Int, StdGen)
-- Returns values in the range stdRange
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
                        -- no statistical foundation for this!
                        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)

-- The global random number generator

{- $globalrng #globalrng#

There is a single, implicit, global random number generator of type
'StdGen', held in some global variable maintained by the 'IO' monad. It is
initialised automatically in some system-dependent fashion, for example, by
using the time of day, or Linux's kernel random number generator. To get
deterministic behaviour, use 'setStdGen'.
-}

-- |Sets the global random number generator.
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

-- |Gets the global random number generator.
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

-- |Applies 'split' to the current global random generator,
-- updates it with one of the results, and returns the other.
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

{- |Uses the supplied function to get a value from the current global
random generator, and updates the global generator with the new generator
returned by the function. For example, @rollDice@ gets a random integer
between 1 and 6:

>  rollDice :: IO Int
>  rollDice = getStdRandom (randomR (1,6))

-}

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)

{- $references

1. FW #Burton# Burton and RL Page, /Distributed random number generation/,
Journal of Functional Programming, 2(2):203-212, April 1992.

2. SK #Park# Park, and KW Miller, /Random number generators -
good ones are hard to find/, Comm ACM 31(10), Oct 1988, pp1192-1201.

3. DG #Carta# Carta, /Two fast implementations of the minimal standard
random number generator/, Comm ACM, 33(1), Jan 1990, pp87-88.

4. P #Hellekalek# Hellekalek, /Don\'t trust parallel Monte Carlo/,
Department of Mathematics, University of Salzburg,
<http://random.mat.sbg.ac.at/~peter/pads98.ps>, 1998.

5. Pierre #LEcuyer# L'Ecuyer, /Efficient and portable combined random
number generators/, Comm ACM, 31(6), Jun 1988, pp742-749.

The Web site <http://random.mat.sbg.ac.at/> is a great source of information.

-}