FsRandom


FsRandom

FsRandom is a purely-functional random number generator framework designed for F# language. It helps you to obtain a variety of random numbers to use more than ten predefined generators, and to define a new function to generate random numbers you want.

How to Install

Install from NuGet

FsRandom is available on the NuGet Gallery. Run in the Package Manager Console:
PM> Install-Package FsRandom

Build from source code

FAKE script is included in the source code. To make a debug build, run:
> fsi tools\build.fsx --debug

Short Example

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open FsRandom

// Random state
let state = createState xorshift (123456789u, 362436069u, 521288629u, 88675123u)

// Random point generator
let randomPointGenerator = random {
   let! x = ``[0, 1)``  // generates a random number between 0 and 1
   let! y = Statistics.normal (0.0, 1.0)  // generates a normal random number
   return (x, y)
}

// Get a random point
let randomPoint = Random.get randomPointGenerator state
printf "(x, y) = (%f, %f)" <|| randomPoint

The script yields:

(x, y) = (0.106706, -1.561583)

Features

Random Functions

FsRandom provides a variety of random number generator functions:

  • RandomNumberGenerator module provides standard random number generators: ``(0, 1)``, ``[0, 1)``, ``(0, 1]``, and ``[0, 1]``.
  • Random module manipulates random numbers.
  • Statistics module provides a variety of statistical distributions such like uniform, normal and gamma.
  • Seq module provides functions for generating random number sequences.
  • Array module and Array2D module provide functions for array operations like createRandom, sample, sampleWithReplacement, and shuffle.
  • List module provides functions for generating random lists.
  • String module provides functions for generating random strings.
  • Utility module provides utility functions.

Pseudo-Random Number Generators

You can choose an algorithm of pseudo-random numbger generator:

  • xorshift implements Xorshift algorithm.
  • systemrandom leverages System.Random and its subclasses as a random number generators. This PRNG is not purely functional because the state of the PRNG is controled in the classes.
  • mersenne in MersenneTwister module implements 64-bit version of Mersenne Twister algorithm.
  • sfmt in SimdOrientedFastMersenneTwister module implements SFMT algorithm.
namespace FsRandom
val state : PrngState

Full name: Index.state
val createState : prng:Prng<'s> -> seed:'s -> PrngState

Full name: FsRandom.RandomNumberGenerator.createState
val xorshift : Prng<uint32 * uint32 * uint32 * uint32>

Full name: FsRandom.RandomNumberGenerator.xorshift
val randomPointGenerator : GeneratorFunction<float * float>

Full name: Index.randomPointGenerator
val random : RandomBuilder

Full name: FsRandom.RandomNumberGenerator.random
val x : float
val ( [0, 1) ) : GeneratorFunction<float>

Full name: FsRandom.RandomNumberGenerator.( [0, 1) )
val y : float
module Statistics

from FsRandom
val normal : mean:float * standardDeviation:float -> GeneratorFunction<float>

Full name: FsRandom.Statistics.normal
val randomPoint : float * float

Full name: Index.randomPoint
module Random

from FsRandom
val get : generator:GeneratorFunction<'a> -> PrngState -> 'a

Full name: FsRandom.Random.get
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
Fork me on GitHub