uniformR package:mwc-random

Generate single uniformly distributed random variable in a given range.
  • For integral types inclusive range is used.
  • For floating point numbers range (a,b] is used if one ignores rounding errors.
Generates a value uniformly distributed over the provided range, which is interpreted as inclusive in the lower and upper bound.
  • uniformRM (1 :: Int, 4 :: Int) generates values uniformly from the set <math>
  • uniformRM (1 :: Float, 4 :: Float) generates values uniformly from the set <math>
The following law should hold to make the function always defined:
uniformRM (a, b) = uniformRM (b, a)
The range is understood as defined by means of isInRange, so
isInRange (a, b) <$> uniformRM (a, b) gen == pure True
but beware of floating point number caveats. There is a default implementation via Generic:
>>> :seti -XDeriveGeneric -XDeriveAnyClass

>>> import GHC.Generics (Generic)

>>> import Data.Word (Word8)

>>> import Control.Monad (replicateM)

>>> import System.Random.Stateful

>>> gen <- newIOGenM (mkStdGen 42)

>>> data Tuple = Tuple Bool Word8 deriving (Show, Generic, UniformRange)

>>> replicateM 10 (uniformRM (Tuple False 100, Tuple True 150) gen)
[Tuple False 102,Tuple True 118,Tuple False 115,Tuple True 113,Tuple True 126,Tuple False 127,Tuple True 130,Tuple False 113,Tuple False 150,Tuple False 125]
The class of types for which a uniformly distributed value can be drawn from a range.