uniformR -package:pcg-random

Generates a value uniformly distributed over the provided range, which is interpreted as inclusive in the lower and upper bound.
  • uniformR (1 :: Int, 4 :: Int) generates values uniformly from the set <math>
  • uniformR (1 :: Float, 4 :: Float) generates values uniformly from the set <math>
The following law should hold to make the function always defined:
uniformR (a, b) = uniformR (b, a)
This is a pure version of uniformRM.

Examples

>>> import System.Random

>>> let pureGen = mkStdGen 137

>>> uniformR (1 :: Int, 4 :: Int) pureGen
(4,StdGen {unStdGen = SMGen 11285859549637045894 7641485672361121627})
You can use type applications to disambiguate the type of the generated numbers:
>>> :seti -XTypeApplications

>>> uniformR @Int (1, 4) pureGen
(4,StdGen {unStdGen = SMGen 11285859549637045894 7641485672361121627})
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.
The uniform distribution over the provided interval.
>>> sample (uniformR (0, 1)) gen
0.44984153252922365
Uniform between two values
Endlessly produce anything that's Variate from a uniform distribution, within some given range of values.
  • For integral types, inclusive range is used.
  • For floating types, (a,b] is used.
Produce an infinite list of pseudo-random values in a specified range. Same as uniforms, integrates nicely with list fusion. There is no way to recover the final generator, therefore either use split before calling uniformRs or use uniformListR instead. Similar to randomRs, except it relies on UniformRange type class instead of Random.

Examples

>>> let gen = mkStdGen 2023

>>> take 5 $ uniformRs (10, 100) gen :: [Int]
[32,86,21,57,39]
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]
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]
Same as uniformArray, but will generate values in a supplied range.
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 class of types for which a uniformly distributed value can be drawn from a range.
The class of types for which a uniformly distributed value can be drawn from a range.
A reservoir in which all samples are equally likely to be evicted when the reservoir is at full capacity. This is conceptually simpler than the ExponentiallyDecayingReservoir, but at the expense of providing a less accurate sample.
Type of a random number generator that uniformily chooses an element from a range.
Generates a bitvector uniformly distributed over the provided range (interpreted as a range of signed bitvectors), which is interpreted as inclusive in the lower and upper bound. (See uniformRM).
Generates a bitvector uniformly distributed over the provided range (interpreted as a range of unsigned bitvectors), which is interpreted as inclusive in the lower and upper bound. (See uniformRM).
randomR implemented by sequencing randomR between all components. If given a range of items made with different constructors, will be error!