FsRandom Documentation
First Random Number
Let's try to get a first random number z ~ N(0, 1).
It is easy to do with normal
random number generator in the Statistics module.
To give the specific parameter, say the mean of 0 and the variance of 1, do:
1:
|
|
The generator function is only able to use with a pseudo-random number generator (PRNG). The PRNG constructs a computation expression to generate random numbers. The computation expression is a function which takes a random seed and returns a random number and a new seed for the next call. It is important to keep the new state because it is used when you generate a new random number.
Here for example, you choose xorshift PRNG, which is implemented in FsRandom.
You need to define an initial random seed first for xorshift algorithm
(of course, another algorithm is available rather than xorshift, as described later).
It is a tuple composed of four 32-bit unsigned integers.
And then, you should combine the PRNG and the seed using createState
function.
1: 2: |
|
FsRandom also provides a default random state and a method to create a random state randomly for ease of use:
1: 2: |
|
Now you can retrieve a random number using Random.get
function.
1: 2: |
|
|
Since Random.get
returns a stateless function,
if you do the code above, the same thing occurs.
1: 2: |
|
|
To generate a new random number,
you need to get next state using Random.next
instead of Random.get
:
1: 2: 3: |
|
|
Transforming Random Numbers
Transformation of random numbers is a regular work.
FsRandom defines Random.map
function for the purpose.
The following code shows how to use it.
1: 2: 3: 4: |
|
plusOne
is a function that takes an argument and adds one to it.
uniform
is a uniform random number generator between its two arguments.
So x
finally becomes a uniform random number between 1 and 2.
The both following codes return the same results as above.
1: 2: 3: 4: |
|
1: 2: 3: 4: |
|
Random Number Sequence
Usually, you use a lot of random numbers for our needs. The following code defines a function generating an infinite binary sequence using Bernoulli random number generator, and it illustrates how you can generate a number of random numbers.
1: 2: 3: 4: 5: |
|
Or, more precisely like the following:
1:
|
|
Using System.Random
The examples above uses xorshift
to generate random numbers.
The familliar System.Random
(and its subclasses) is available in the workflow.
Just use systemrandom
instead of xorshift
.
1: 2: |
|
Because System.Random
is a stateful object,
unlike xorshift
, you will get different result on each call.
1: 2: |
|
Generator function
This section explains how to construct generator functions such like normal
and uniform
.
The type of generator function is GeneratorFunction<'a>
,
where 'a
is a type of random numbers the generator function returns.
As an example of user-defined generator function, let's construct a random number generator to produce an approximate standard normal random number (approximately ~ N(0, 1)). Theorem says that the mean of 12 standard random numbers, namely, 12 random numbers between 0 and 1, approximates a normal random number with mean of 1/2 and variance of 1/12. Therefore, if you subtract 6 from the sum of 12 standard random numbers, the result approximates a standard normal random number.
1: 2: 3: 4: |
|
The approximatelyStandardNormal
can be used in the generating process as the following.
1:
|
|
Don't forget that FsRandom has a normal random number generator normal
.
Pseudo-random number generators
This section explains how to implement pseudo-random number generator (PRNG) algorithms such as xorshift
and systemrandom
.
A PRNG is often defined as a simple series of numbers whose next number is determined by the current state.
For example, the Xorshift algorithm has four 32-bit integers as a state.
To describe such PRNGs, the type of PRNGs in FsRandom is defined as type Prng<'s> = 's -> uint64 * 's
.
Here 's
is the type of random state of the PRNG.
As an example of user-defined Prng
,
let's implement linear congruential generator.
First, you make a function of Prng
.
1: 2: |
|
The first returned value is a random number and the second returned value is a next state.
Note that modulus is not defined because Prng
is required to return random numbers
in 64-bit resolution.
Hereafter you can use the linear
PRNG to generate random numbers.
1: 2: |
|
Full name: Documentation.generator
from FsRandom
Full name: FsRandom.Statistics.normal
Full name: Documentation.seed
Full name: Documentation.state
Full name: FsRandom.RandomNumberGenerator.createState
Full name: FsRandom.RandomNumberGenerator.xorshift
from FsRandom
Full name: FsRandom.Utility.defaultState
Full name: FsRandom.Utility.createRandomState
Full name: Documentation.z1
from FsRandom
Full name: FsRandom.Random.get
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Full name: Documentation.z2
Full name: Documentation.nextState
Full name: FsRandom.Random.next
Full name: Documentation.z3
Full name: Documentation.plusOne
Full name: FsRandom.Random.map
Full name: FsRandom.Statistics.uniform
Full name: FsRandom.Random.identity
Full name: Documentation.binaries
val seq : sequence:seq<'T> -> seq<'T>
Full name: Microsoft.FSharp.Core.Operators.seq
--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>
Full name: Microsoft.FSharp.Collections.seq<_>
Full name: FsRandom.Statistics.bernoulli
Full name: Documentation.binaries2
module Seq
from FsRandom
--------------------
module Seq
from Microsoft.FSharp.Collections
Full name: FsRandom.Seq.ofRandom
Full name: Documentation.r0
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float
Full name: System.Random
--------------------
System.Random() : unit
System.Random(Seed: int) : unit
Full name: Documentation.s
Full name: FsRandom.RandomNumberGenerator.systemrandom
Full name: Documentation.u1
Full name: Documentation.u2
Full name: Documentation.approximatelyStandardNormal
Full name: FsRandom.RandomNumberGenerator.random
module Array
from FsRandom
--------------------
module Array
from Microsoft.FSharp.Collections
Full name: FsRandom.Array.randomCreate
Full name: FsRandom.RandomNumberGenerator.( (0, 1) )
Full name: Microsoft.FSharp.Collections.Array.sum
Full name: Documentation.linear
Full name: Documentation.linearState