module Rand: sig end
So, the *Dist modules are two-argument functors for the most part. The first parameter is one of the ones in the Math module (Or a user-provided one that has a compatible signature), that tells the code how to do basic math on a numeric type. The second parameter is a source of random numbers. Numbers generated by it are fiddled with by the Dist module to get the proper distribution.
There are several Source modules in the MtRand and FileRand packages, and SysSource in this file is a Source for the standard Random generator.
Each fully functorized module has two functions (min
and max
,
aliases for Source.min
and Source.max
), and one class,
rng
. rng
's constructor arguments vary depending on the
distribution. The class has three methods: min and max, for the
lowest and highest numbers an object will return, and genrand, which
returns one random number according to the proper distribution.
Note that these distributions do not do any seeding of the underlying Source generator.
They also haven't been tested exceedingly well. Most of the
implementations are based on algorithms and descriptions from
Knuth's The Art of Computer Programming, but I might have made
errors in the translation. One of these days I'll have to look into
doing some real testing to make sure the distributions are accurate.
Example:
Set up a new module that works with ints using the Mersenne Twister:
module MyRNG = Rand.UniformDist2(Math.IntOps)(MtRand.IntSource)
Create a uniform RNG that returns values in the range $0\leq x<100$ and print one number:
let myrand = new MyRNG.rng 0 100 in print_int myrand#genrand;
print_newline ()
\bigskip
module type RNGSource = sig end
module S2USSource: functor (Ops : Math.Ops) -> functor (Source : sig end) -> sig end
module SysSource: sig end
Random.int
generator.
module SysFloatSource: sig end
module type IDIST = functor (Ops : sig end) -> functor (Source : sig end) -> sig end
module UniformDist: IDIST
new UniformDist.rng low high
returns an object that will generate
random numbers in the range low < x < high.
module UniformDist2: IDIST
new UniformDist2.rng low high
returns an object that will
generate random numbers in the range low <= x < high.
module type IFDIST = functor (Ops : sig end) -> functor (Source : sig end) -> sig end
module GeometricDist: IFDIST
new GeometricDist.rng mean
returns an object that will generate
random numbers in the geometric distribution using the given mean.
module PoissonDist: IFDIST
new PoissonDist.rng mean
returns an object that will generate
random numbers in the Poisson distribution using the given mean.
module type FDIST = functor (Source : sig end) -> sig end
module NormalDist: FDIST
new NormalDist.rng
returns an object that will generate random
floats in the normal distribution (Mean 0, standard deviation 1).
module ExponentialDist: functor (Source : sig end) -> sig end
new ExponentialDist.rng mean
returns an object that will generate
random floats in an exponential distribution with the given mean.