From 9a5ecea3acff396bc995660f33f9bdb48415a47d Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 6 Oct 2013 15:43:58 +0200 Subject: [PATCH] Distributions: update F# module to core changes; easier to specify just seed --- src/FSharp/Distributions.fs | 17 ---------------- src/FSharp/Random.fs | 10 +++++++++- src/FSharpExamples/RandomAndDistributions.fsx | 20 +++++++++---------- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/src/FSharp/Distributions.fs b/src/FSharp/Distributions.fs index 91d03a6d..e5e5aa03 100644 --- a/src/FSharp/Distributions.fs +++ b/src/FSharp/Distributions.fs @@ -32,23 +32,6 @@ namespace MathNet.Numerics.Distributions open MathNet.Numerics.Random -[] -[] -module Distributions = - - let withRandom random (dist:#IUnivariateDistribution) = - dist.RandomSource <- random - dist - - let withSystemRandom dist = dist |> withRandom (Random.system()) - let withMersenneTwister dist = dist |> withRandom (Random.mersenneTwister()) - -#if PORTABLE -#else - let withCryptoRandom dist = dist |> withRandom (Random.crypto()) -#endif - - [] module Sample = diff --git a/src/FSharp/Random.fs b/src/FSharp/Random.fs index 2db22093..388edb9f 100644 --- a/src/FSharp/Random.fs +++ b/src/FSharp/Random.fs @@ -45,7 +45,7 @@ module Random = /// Creates a default .Net system pRNG with a custom seed based on uinque GUIDs let system () = new System.Random(seed()) - let systemWith seed = new System.Random(seed) + let systemSeed seed = new System.Random(seed) #if PORTABLE #else @@ -56,34 +56,42 @@ module Random = /// Creates a Mersenne Twister 19937 pRNG with a custom seed based on uinque GUIDs let mersenneTwister () = new MersenneTwister(seed()) :> System.Random + let mersenneTwisterSeed (seed:int) = new MersenneTwister(seed) :> System.Random let mersenneTwisterWith seed threadSafe = new MersenneTwister(seed, threadSafe) :> System.Random /// Creates a multiply-with-carry Xorshift (Xn = a * Xn−3 + c mod 2^32) pRNG with a custom seed based on uinque GUIDs let xorshift () = new Xorshift(seed()) :> System.Random + let xorshiftSeed (seed:int) = new Xorshift(seed) :> System.Random let xorshiftWith seed threadSafe = new Xorshift(seed, threadSafe) :> System.Random let xorshiftCustom seed threadSafe a c x1 x2 = new Xorshift(seed, threadSafe, a, c, x1, x2) :> System.Random /// Creates a Wichmann-Hill’s 1982 combined multiplicative congruential pRNG with a custom seed based on uinque GUIDs let wh1982 () = new WH1982(seed()) :> System.Random + let wh1982Seed (seed:int) = new WH1982(seed) :> System.Random let wh1982With seed threadSafe = new WH1982(seed, threadSafe) :> System.Random /// Creates a Wichmann-Hill’s 2006 combined multiplicative congruential pRNG with a custom seed based on uinque GUIDs let wh2006 () = new WH2006(seed()) :> System.Random + let wh2006Seed (seed:int) = new WH2006(seed) :> System.Random let wh2006With seed threadSafe = new WH2006(seed, threadSafe) :> System.Random /// Creates a Parallel Additive Lagged Fibonacci pRNG with a custom seed based on uinque GUIDs let palf () = new Palf(seed()) :> System.Random + let palfSeed (seed:int) = new Palf(seed) :> System.Random let palfWith seed threadSafe = new Palf(seed, threadSafe, 418, 1279) :> System.Random let palfCustom seed threadSafe shortLag longLag = new Palf(seed, threadSafe, shortLag, longLag) :> System.Random /// Creates a Multiplicative congruential generator using a modulus of 2^59 and a multiplier of 13^13 pRNG with a custom seed based on uinque GUIDs let mcg59 () = new Mcg59(seed()) :> System.Random + let mcg59Seed (seed:int) = new Mcg59(seed) :> System.Random let mcg59With seed threadSafe = new Mcg59(seed, threadSafe) :> System.Random /// Creates a Multiplicative congruential generator using a modulus of 2^31-1 and a multiplier of 1132489760 pRNG with a custom seed based on uinque GUIDs let mcg31m1 () = new Mcg31m1(seed()) :> System.Random + let mcg31m1Seed (seed:int) = new Mcg31m1(seed) :> System.Random let mcg31m1With seed threadSafe = new Mcg31m1(seed, threadSafe) :> System.Random /// Creates a 32-bit combined multiple recursive generator with 2 components of order 3 pRNG with a custom seed based on uinque GUIDs let mrg32k3a () = new Mrg32k3a(seed()) :> System.Random + let mrg32k3aSeed (seed:int) = new Mrg32k3a(seed) :> System.Random let mrg32k3aWith seed threadSafe = new Mrg32k3a(seed, threadSafe) :> System.Random diff --git a/src/FSharpExamples/RandomAndDistributions.fsx b/src/FSharpExamples/RandomAndDistributions.fsx index 23bcac37..dc2788ab 100644 --- a/src/FSharpExamples/RandomAndDistributions.fsx +++ b/src/FSharpExamples/RandomAndDistributions.fsx @@ -34,13 +34,13 @@ open MathNet.Numerics.Random open MathNet.Numerics.Distributions -// generate some seeds for random values +// generate some seeds for random values (NOT intended for cryptography!) let someGuidSeed = Random.seed () let someTimeSeed = Random.timeSeed () -// generate some pseudo random number generators (listing incomplete; all of them are cast to the common base type, System.Random) +// some pseudo random number generators (listing incomplete; all of them are cast to the common base type, System.Random) let a = Random.system () -let b = Random.systemWith (Random.timeSeed()) +let b = Random.systemSeed (Random.timeSeed()) let c = Random.crypto () let d = Random.mersenneTwister () let e = Random.mersenneTwisterWith 1000 true (* thread-safe *) @@ -59,13 +59,13 @@ let values = ( f.NextDecimal() ) -// generate some probability distributions -let normal = Normal.WithMeanVariance(3.0, 1.5) |> withRandom g -let exponential = new Exponential(2.4) -let gamma = new Gamma(2.0, 1.5) |> withCryptoRandom -let cauchy = new Cauchy() |> withRandom (Random.mrg32k3aWith 10 false) -let poisson = new Poisson(3.0) -let geometric = new Geometric(0.8) |> withSystemRandom +// some probability distributions +let normal = Normal.WithMeanVariance(3.0, 1.5, g) +let exponential = Exponential(2.4) +let gamma = Gamma(2.0, 1.5, Random.crypto()) +let cauchy = Cauchy(0.0, 1.0, Random.mrg32k3aWith 10 false) +let poisson = Poisson(3.0) +let geometric = Geometric(0.8, Random.system()) // generate some random samples from these distributions let continuous = [