random package:MonadRandom

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 floating point types, the range is normally the closed interval [0,1].
  • For Integer, the range is (arbitrarily) the range of Int.
This module is provided for backwards compatibility, and simply re-exports Control.Monad.Random.Lazy.
The class of types for which random values can be generated. Most instances of Random will produce values that are uniformly distributed on the full range, but for those types without a well-defined "full range" some sensible default subrange will be selected. Random exists primarily for backwards compatibility with version 1.1 of this library. In new code, use the better specified Uniform and UniformRange instead.
Random monads, passing a random number generator through a computation. This version is lazy; for a strict version, see Control.Monad.Trans.Random.Strict, which has the same interface.
A variant of randomM that uses the global pseudo-random number generator globalStdGen.
>>> import Data.Int

>>> randomIO :: IO Int32
114794456
This function is equivalent to getStdRandom random and is included in this interface for historical reasons and backwards compatibility. It is recommended to use uniformM instead, possibly with the globalStdGen if relying on the global state is acceptable.
>>> import System.Random.Stateful

>>> uniformM globalStdGen :: IO Int32
-1768545016
Takes a range (lo,hi) and a pseudo-random number generator g, and returns a pseudo-random value uniformly distributed over the closed interval [lo,hi], together with a new generator. It is unspecified what happens if lo>hi, but usually the values will simply get swapped.
>>> let gen = mkStdGen 26

>>> fst $ randomR ('a', 'z') gen
'z'

>>> fst $ randomR ('a', 'z') gen
'z'
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. There is no requirement to follow the Ord instance and the concept of range can be defined on per type basis. For example product types will treat their values independently:
>>> fst $ randomR (('a', 5.0), ('z', 10.0)) $ mkStdGen 26
('z',5.22694980853051)
In case when a lawful range is desired uniformR should be used instead.
A variant of randomRM that uses the global pseudo-random number generator globalStdGen
>>> randomRIO (2020, 2100) :: IO Int
2028
Similar to randomIO, this function is equivalent to getStdRandom randomR and is included in this interface for historical reasons and backwards compatibility. It is recommended to use uniformRM instead, possibly with the globalStdGen if relying on the global state is acceptable.
>>> import System.Random.Stateful

>>> uniformRM (2020, 2100) globalStdGen :: IO Int
2044
Plural variant of randomR, producing an infinite list of pseudo-random values instead of returning a new generator.
Plural variant of random, producing an infinite list of pseudo-random values instead of returning a new generator.
RandomGen is an interface to pure pseudo-random number generators. StdGen is the standard RandomGen instance provided by this library.
Random-number generation monad. Support for computations which consume random values.
With a source of random number supply in hand, the MonadRandom class allows the programmer to extract random values of a variety of types.
The same as getRandomR, 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.
See random for details.
Takes a range (lo,hi) and a random number generator g, and returns a computation that 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. See randomR for details.
Plural variant of getRandomR, producing an infinite list of random values instead of returning a new generator. See randomRs for details.
Plural variant of getRandom, producing an infinite list of random values instead of returning a new generator. See randoms for details.
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 produces a pseudo-random integer between 1 and 6:
>>> rollDice = getStdRandom (randomR (1, 6))

>>> replicateM 10 (rollDice :: IO Int)
[1,1,1,4,5,6,1,2,2,5]
This is an outdated function and it is recommended to switch to its equivalent applyAtomicGen instead, possibly with the globalStdGen if relying on the global state is acceptable.
>>> import System.Random.Stateful

>>> rollDice = applyAtomicGen (uniformR (1, 6)) globalStdGen

>>> replicateM 10 (rollDice :: IO Int)
[2,1,1,5,4,3,6,6,3,2]