{-
- ``Data/Random/Source''
-}{-# LANGUAGE
MultiParamTypeClasses, FlexibleInstances
#-}moduleData.Random.Source(MonadRandom(..),RandomSource(..))whereimportData.WordimportControl.MonadimportData.Random.Internal.Words-- |A typeclass for monads with a chosen source of entropy. For example,-- 'RVar' is such a monad - the source from which it is (eventually) sampled-- is the only source from which a random variable is permitted to draw, so-- when directly requesting entropy for a random variable these functions-- are used.-- -- The minimal definition is either 'getRandomByte' or 'getRandomWord'.-- 'getRandomDouble' is defaulted in terms of 'getRandomWord'.classMonadm=>MonadRandommwhere-- |Get a random uniformly-distributed byte.getRandomByte::mWord8getRandomByte=doword<-getRandomWordreturn(fromIntegralword)-- |Get a random 'Word64' uniformly-distributed over the full range of the type.getRandomWord::mWord64getRandomWord=dob0<-getRandomByteb1<-getRandomByteb2<-getRandomByteb3<-getRandomByteb4<-getRandomByteb5<-getRandomByteb6<-getRandomByteb7<-getRandomBytereturn(buildWordb0b1b2b3b4b5b6b7)-- |Get a random 'Double' uniformly-distributed over the interval [0,1)getRandomDouble::mDoublegetRandomDouble=doword<-getRandomWordreturn(wordToDoubleword)-- |A source of entropy which can be used in the given monad.---- The minimal definition is either 'getRandomByteFrom' or 'getRandomWordFrom'.-- 'getRandomDoubleFrom' is defaulted in terms of 'getRandomWordFrom'classMonadm=>RandomSourcemswhere-- |Get a random uniformly-distributed byte.getRandomByteFrom::s->mWord8getRandomByteFromsrc=doword<-getRandomWordFromsrcreturn(fromIntegralword)-- |Get a random 'Word64' uniformly-distributed over the full range of the type.getRandomWordFrom::s->mWord64getRandomWordFromsrc=dob0<-getRandomByteFromsrcb1<-getRandomByteFromsrcb2<-getRandomByteFromsrcb3<-getRandomByteFromsrcb4<-getRandomByteFromsrcb5<-getRandomByteFromsrcb6<-getRandomByteFromsrcb7<-getRandomByteFromsrcreturn(buildWordb0b1b2b3b4b5b6b7)-- |Get a random 'Double' uniformly-distributed over the interval [0,1)getRandomDoubleFrom::s->mDoublegetRandomDoubleFromsrc=doword<-getRandomWordFromsrcreturn(wordToDoubleword)instanceMonadm=>RandomSourcem(mWord8)wheregetRandomByteFrom=idinstanceMonadm=>RandomSourcem(mWord64)wheregetRandomWordFrom=id