From 6d7b0e3f2ece70c2d2fa06896bff2b2642a9e8cb Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sun, 6 Oct 2013 15:41:43 +0200 Subject: [PATCH] Random: use a less problematic seed by default --- src/FSharp/Random.fs | 12 ++++++--- src/Numerics/Distributions/Bernoulli.cs | 6 ++--- src/Numerics/Distributions/Beta.cs | 6 ++--- src/Numerics/Distributions/Binomial.cs | 6 ++--- src/Numerics/Distributions/Categorical.cs | 8 +++--- src/Numerics/Distributions/Cauchy.cs | 6 ++--- src/Numerics/Distributions/Chi.cs | 6 ++--- src/Numerics/Distributions/ChiSquared.cs | 6 ++--- .../Distributions/ContinuousUniform.cs | 6 ++--- .../Distributions/ConwayMaxwellPoisson.cs | 6 ++--- src/Numerics/Distributions/Dirichlet.cs | 10 +++---- src/Numerics/Distributions/DiscreteUniform.cs | 6 ++--- src/Numerics/Distributions/Erlang.cs | 6 ++--- src/Numerics/Distributions/Exponential.cs | 6 ++--- src/Numerics/Distributions/FisherSnedecor.cs | 6 ++--- src/Numerics/Distributions/Gamma.cs | 6 ++--- src/Numerics/Distributions/Geometric.cs | 6 ++--- src/Numerics/Distributions/Hypergeometric.cs | 6 ++--- src/Numerics/Distributions/InverseGamma.cs | 6 ++--- src/Numerics/Distributions/InverseWishart.cs | 6 ++--- src/Numerics/Distributions/Laplace.cs | 6 ++--- src/Numerics/Distributions/LogNormal.cs | 6 ++--- src/Numerics/Distributions/MatrixNormal.cs | 6 ++--- src/Numerics/Distributions/Multinomial.cs | 8 +++--- .../Distributions/NegativeBinomial.cs | 6 ++--- src/Numerics/Distributions/Normal.cs | 6 ++--- src/Numerics/Distributions/NormalGamma.cs | 6 ++--- src/Numerics/Distributions/Pareto.cs | 6 ++--- src/Numerics/Distributions/Poisson.cs | 6 ++--- src/Numerics/Distributions/Rayleigh.cs | 6 ++--- src/Numerics/Distributions/Stable.cs | 6 ++--- src/Numerics/Distributions/StudentT.cs | 6 ++--- src/Numerics/Distributions/Weibull.cs | 6 ++--- src/Numerics/Distributions/Wishart.cs | 4 +-- src/Numerics/Distributions/Zipf.cs | 6 ++--- src/Numerics/Numerics.csproj | 1 + src/Numerics/Random/RandomSeed.cs | 27 +++++++++++++++++++ src/Numerics/Statistics/MCMC/HybridMC.cs | 10 +++---- src/Numerics/Statistics/MCMC/MCMCSampler.cs | 12 ++++----- .../Statistics/MCMC/UnivariateHybridMC.cs | 8 +++--- 40 files changed, 154 insertions(+), 126 deletions(-) create mode 100644 src/Numerics/Random/RandomSeed.cs diff --git a/src/FSharp/Random.fs b/src/FSharp/Random.fs index 327e9e3d..2db22093 100644 --- a/src/FSharp/Random.fs +++ b/src/FSharp/Random.fs @@ -33,11 +33,15 @@ namespace MathNet.Numerics.Random [] module Random = - /// Provides a seed based on unique GUIDs - let seed () = System.Guid.NewGuid().GetHashCode() + /// Provides a time-dependent seed value (default behavior of System.Random) + /// WARNING: There is no randomness in this seed and quick repeated calls can cause + /// the same seed value. Do not use for cryptography! + let timeSeed () = RandomSeed.Time() - /// Provides a time-dependent seed value (caution, can produce the same value on quick repeated execution) - let timeSeed () = System.Environment.TickCount + /// Provides a seed based on unique GUIDs + /// WARNING: There is only low randomness in this seed, but at least quick repeated + /// calls will result in different seed values. Do not use for cryptography! + let seed () = RandomSeed.Guid() /// Creates a default .Net system pRNG with a custom seed based on uinque GUIDs let system () = new System.Random(seed()) diff --git a/src/Numerics/Distributions/Bernoulli.cs b/src/Numerics/Distributions/Bernoulli.cs index 4ed6a44c..2ca62981 100644 --- a/src/Numerics/Distributions/Bernoulli.cs +++ b/src/Numerics/Distributions/Bernoulli.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.Distributions /// If the Bernoulli parameter is not in the range [0,1]. public Bernoulli(double p) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(p); } @@ -70,7 +70,7 @@ namespace MathNet.Numerics.Distributions /// If the Bernoulli parameter is not in the range [0,1]. public Bernoulli(double p, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(p); } @@ -123,7 +123,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Beta.cs b/src/Numerics/Distributions/Beta.cs index 7f27caa1..4f060cee 100644 --- a/src/Numerics/Distributions/Beta.cs +++ b/src/Numerics/Distributions/Beta.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.Distributions /// The β shape parameter of the Beta distribution. Range: β ≥ 0. public Beta(double a, double b) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(a, b); } @@ -77,7 +77,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Beta(double a, double b, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(a, b); } @@ -131,7 +131,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Binomial.cs b/src/Numerics/Distributions/Binomial.cs index 279e36d6..6d04e3c0 100644 --- a/src/Numerics/Distributions/Binomial.cs +++ b/src/Numerics/Distributions/Binomial.cs @@ -61,7 +61,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Binomial(double p, int n) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(p, n); } @@ -75,7 +75,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Binomial(double p, int n, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(p, n); } @@ -141,7 +141,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Categorical.cs b/src/Numerics/Distributions/Categorical.cs index 3e917ec8..82f29932 100644 --- a/src/Numerics/Distributions/Categorical.cs +++ b/src/Numerics/Distributions/Categorical.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.Distributions /// If any of the probabilities are negative or do not sum to one. public Categorical(double[] probabilityMass) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(probabilityMass); } @@ -78,7 +78,7 @@ namespace MathNet.Numerics.Distributions /// If any of the probabilities are negative or do not sum to one. public Categorical(double[] probabilityMass, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(probabilityMass); } @@ -104,7 +104,7 @@ namespace MathNet.Numerics.Distributions p[i] = histogram[i].Count; } - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(p); } @@ -207,7 +207,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Cauchy.cs b/src/Numerics/Distributions/Cauchy.cs index 3435834f..cda19f58 100644 --- a/src/Numerics/Distributions/Cauchy.cs +++ b/src/Numerics/Distributions/Cauchy.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.Distributions /// The scale (γ) of the distribution. Range: γ > 0. public Cauchy(double location, double scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale); } @@ -77,7 +77,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Cauchy(double location, double scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale); } @@ -131,7 +131,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Chi.cs b/src/Numerics/Distributions/Chi.cs index 14c4d586..d6fe0a31 100644 --- a/src/Numerics/Distributions/Chi.cs +++ b/src/Numerics/Distributions/Chi.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (k) of the distribution. Range: k > 0. public Chi(double freedom) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(freedom); } @@ -69,7 +69,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Chi(double freedom, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(freedom); } @@ -112,7 +112,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/ChiSquared.cs b/src/Numerics/Distributions/ChiSquared.cs index 07e69eb0..8a85050d 100644 --- a/src/Numerics/Distributions/ChiSquared.cs +++ b/src/Numerics/Distributions/ChiSquared.cs @@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (k) of the distribution. Range: k > 0. public ChiSquared(double freedom) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(freedom); } @@ -67,7 +67,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public ChiSquared(double freedom, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(freedom); } @@ -110,7 +110,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/ContinuousUniform.cs b/src/Numerics/Distributions/ContinuousUniform.cs index 2cab6128..b62b8ff5 100644 --- a/src/Numerics/Distributions/ContinuousUniform.cs +++ b/src/Numerics/Distributions/ContinuousUniform.cs @@ -66,7 +66,7 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the lower bound. public ContinuousUniform(double lower, double upper) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(lower, upper); } @@ -79,7 +79,7 @@ namespace MathNet.Numerics.Distributions /// If the upper bound is smaller than the lower bound. public ContinuousUniform(double lower, double upper, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(lower, upper); } @@ -133,7 +133,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs index 17c45e3a..cd932321 100644 --- a/src/Numerics/Distributions/ConwayMaxwellPoisson.cs +++ b/src/Numerics/Distributions/ConwayMaxwellPoisson.cs @@ -86,7 +86,7 @@ namespace MathNet.Numerics.Distributions /// The rate of decay (ν) parameter. Range: ν ≥ 0. public ConwayMaxwellPoisson(double lambda, double nu) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(lambda, nu); } @@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public ConwayMaxwellPoisson(double lambda, double nu, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(lambda, nu); } @@ -163,7 +163,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Dirichlet.cs b/src/Numerics/Distributions/Dirichlet.cs index 98151366..72c9d607 100644 --- a/src/Numerics/Distributions/Dirichlet.cs +++ b/src/Numerics/Distributions/Dirichlet.cs @@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions /// An array with the Dirichlet parameters. public Dirichlet(double[] alpha) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(alpha); } @@ -68,7 +68,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Dirichlet(double[] alpha, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(alpha); } @@ -86,7 +86,7 @@ namespace MathNet.Numerics.Distributions parm[i] = alpha; } - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(parm); } @@ -105,7 +105,7 @@ namespace MathNet.Numerics.Distributions parm[i] = alpha; } - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(parm); } @@ -179,7 +179,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/DiscreteUniform.cs b/src/Numerics/Distributions/DiscreteUniform.cs index 23312582..623017a4 100644 --- a/src/Numerics/Distributions/DiscreteUniform.cs +++ b/src/Numerics/Distributions/DiscreteUniform.cs @@ -59,7 +59,7 @@ namespace MathNet.Numerics.Distributions /// Upper bound. Range: lower ≤ upper. public DiscreteUniform(int lower, int upper) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(lower, upper); } @@ -71,7 +71,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public DiscreteUniform(int lower, int upper, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(lower, upper); } @@ -138,7 +138,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Erlang.cs b/src/Numerics/Distributions/Erlang.cs index 2eec3d8c..5c729119 100644 --- a/src/Numerics/Distributions/Erlang.cs +++ b/src/Numerics/Distributions/Erlang.cs @@ -59,7 +59,7 @@ namespace MathNet.Numerics.Distributions /// The rate or inverse scale (λ) of the Erlang distribution. Range: λ ≥ 0. public Erlang(int shape, double rate) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, rate); } @@ -71,7 +71,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Erlang(int shape, double rate, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, rate); } @@ -166,7 +166,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Exponential.cs b/src/Numerics/Distributions/Exponential.cs index d06740e2..2779e605 100644 --- a/src/Numerics/Distributions/Exponential.cs +++ b/src/Numerics/Distributions/Exponential.cs @@ -56,7 +56,7 @@ namespace MathNet.Numerics.Distributions /// The rate (λ) parameter of the distribution. Range: λ ≥ 0. public Exponential(double rate) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(rate); } @@ -67,7 +67,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Exponential(double rate, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(rate); } @@ -110,7 +110,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/FisherSnedecor.cs b/src/Numerics/Distributions/FisherSnedecor.cs index cc0cfe17..efd9b2c0 100644 --- a/src/Numerics/Distributions/FisherSnedecor.cs +++ b/src/Numerics/Distributions/FisherSnedecor.cs @@ -58,7 +58,7 @@ namespace MathNet.Numerics.Distributions /// The second degree of freedom (d2) of the distribution. Range: d2 > 0. public FisherSnedecor(double d1, double d2) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(d1, d2); } @@ -70,7 +70,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public FisherSnedecor(double d1, double d2, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(d1, d2); } @@ -124,7 +124,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Gamma.cs b/src/Numerics/Distributions/Gamma.cs index fd2fef04..b9c3a774 100644 --- a/src/Numerics/Distributions/Gamma.cs +++ b/src/Numerics/Distributions/Gamma.cs @@ -66,7 +66,7 @@ namespace MathNet.Numerics.Distributions /// The rate or inverse scale (β) of the Gamma distribution. Range: β ≥ 0. public Gamma(double shape, double rate) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, rate); } @@ -78,7 +78,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Gamma(double shape, double rate, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, rate); } @@ -173,7 +173,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Geometric.cs b/src/Numerics/Distributions/Geometric.cs index 8713d4ec..5a546734 100644 --- a/src/Numerics/Distributions/Geometric.cs +++ b/src/Numerics/Distributions/Geometric.cs @@ -57,7 +57,7 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of generating one. Range: 0 ≤ p ≤ 1. public Geometric(double p) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(p); } @@ -68,7 +68,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Geometric(double p, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(p); } @@ -121,7 +121,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Hypergeometric.cs b/src/Numerics/Distributions/Hypergeometric.cs index fa0dfb6c..b29f6725 100644 --- a/src/Numerics/Distributions/Hypergeometric.cs +++ b/src/Numerics/Distributions/Hypergeometric.cs @@ -63,7 +63,7 @@ namespace MathNet.Numerics.Distributions /// The number of draws without replacement (n). public Hypergeometric(int population, int success, int draws) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(population, success, draws); } @@ -76,7 +76,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Hypergeometric(int population, int success, int draws, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(population, success, draws); } @@ -128,7 +128,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/InverseGamma.cs b/src/Numerics/Distributions/InverseGamma.cs index de538751..90a227cd 100644 --- a/src/Numerics/Distributions/InverseGamma.cs +++ b/src/Numerics/Distributions/InverseGamma.cs @@ -60,7 +60,7 @@ namespace MathNet.Numerics.Distributions /// The scale (β) of the distribution. Range: β > 0. public InverseGamma(double shape, double scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, scale); } @@ -72,7 +72,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public InverseGamma(double shape, double scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, scale); } @@ -126,7 +126,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/InverseWishart.cs b/src/Numerics/Distributions/InverseWishart.cs index 54c0959f..47d5ac8a 100644 --- a/src/Numerics/Distributions/InverseWishart.cs +++ b/src/Numerics/Distributions/InverseWishart.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.Distributions /// The scale matrix (Ψ) for the inverse Wishart distribution. public InverseWishart(double degreesOfFreedom, Matrix scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(degreesOfFreedom, scale); } @@ -77,7 +77,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public InverseWishart(double degreesOfFreedom, Matrix scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(degreesOfFreedom, scale); } @@ -156,7 +156,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Laplace.cs b/src/Numerics/Distributions/Laplace.cs index 225f43fb..5688bb19 100644 --- a/src/Numerics/Distributions/Laplace.cs +++ b/src/Numerics/Distributions/Laplace.cs @@ -69,7 +69,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Laplace(double location, double scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale); } @@ -82,7 +82,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Laplace(double location, double scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale); } @@ -136,7 +136,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/LogNormal.cs b/src/Numerics/Distributions/LogNormal.cs index b7d71518..4f7f9f3c 100644 --- a/src/Numerics/Distributions/LogNormal.cs +++ b/src/Numerics/Distributions/LogNormal.cs @@ -62,7 +62,7 @@ namespace MathNet.Numerics.Distributions /// The shape (σ) of the logarithm of the distribution. Range: σ ≥ 0. public LogNormal(double mu, double sigma) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(mu, sigma); } @@ -76,7 +76,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public LogNormal(double mu, double sigma, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(mu, sigma); } @@ -168,7 +168,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/MatrixNormal.cs b/src/Numerics/Distributions/MatrixNormal.cs index f70db231..bedd6b17 100644 --- a/src/Numerics/Distributions/MatrixNormal.cs +++ b/src/Numerics/Distributions/MatrixNormal.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Distributions /// If the dimensions of the mean and two covariance matrices don't match. public MatrixNormal(Matrix m, Matrix v, Matrix k) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(m, v, k); } @@ -88,7 +88,7 @@ namespace MathNet.Numerics.Distributions /// If the dimensions of the mean and two covariance matrices don't match. public MatrixNormal(Matrix m, Matrix v, Matrix k, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(m, v, k); } @@ -198,7 +198,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Multinomial.cs b/src/Numerics/Distributions/Multinomial.cs index 01388f3d..7fb992e1 100644 --- a/src/Numerics/Distributions/Multinomial.cs +++ b/src/Numerics/Distributions/Multinomial.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Multinomial(double[] p, int n) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(p, n); } @@ -89,7 +89,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Multinomial(double[] p, int n, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(p, n); } @@ -118,7 +118,7 @@ namespace MathNet.Numerics.Distributions } SetParameters(p, n); - RandomSource = new System.Random(); + RandomSource = new System.Random(Random.RandomSeed.Guid()); } /// @@ -201,7 +201,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/NegativeBinomial.cs b/src/Numerics/Distributions/NegativeBinomial.cs index a0368ce5..8ca52d43 100644 --- a/src/Numerics/Distributions/NegativeBinomial.cs +++ b/src/Numerics/Distributions/NegativeBinomial.cs @@ -60,7 +60,7 @@ namespace MathNet.Numerics.Distributions /// The probability (p) of a trial resulting in success. Range: 0 ≤ p ≤ 1. public NegativeBinomial(double r, double p) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(r, p); } @@ -72,7 +72,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public NegativeBinomial(double r, double p, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(r, p); } @@ -139,7 +139,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// /// Gets the mean of the distribution. diff --git a/src/Numerics/Distributions/Normal.cs b/src/Numerics/Distributions/Normal.cs index 6aa13167..ef645a60 100644 --- a/src/Numerics/Distributions/Normal.cs +++ b/src/Numerics/Distributions/Normal.cs @@ -81,7 +81,7 @@ namespace MathNet.Numerics.Distributions /// The standard deviation (σ) of the normal distribution. Range: σ ≥ 0. public Normal(double mean, double stddev) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(mean, stddev); } @@ -94,7 +94,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Normal(double mean, double stddev, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(mean, stddev); } @@ -224,7 +224,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/NormalGamma.cs b/src/Numerics/Distributions/NormalGamma.cs index 5f6d7c28..9b1c27b6 100644 --- a/src/Numerics/Distributions/NormalGamma.cs +++ b/src/Numerics/Distributions/NormalGamma.cs @@ -118,7 +118,7 @@ namespace MathNet.Numerics.Distributions /// The inverse scale of the precision. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); } @@ -132,7 +132,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public NormalGamma(double meanLocation, double meanScale, double precisionShape, double precisionInverseScale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(meanLocation, meanScale, precisionShape, precisionInverseScale); } @@ -222,7 +222,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Pareto.cs b/src/Numerics/Distributions/Pareto.cs index 3b546ac1..5092475c 100644 --- a/src/Numerics/Distributions/Pareto.cs +++ b/src/Numerics/Distributions/Pareto.cs @@ -61,7 +61,7 @@ namespace MathNet.Numerics.Distributions /// If or are negative. public Pareto(double scale, double shape) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(scale, shape); } @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Distributions /// If or are negative. public Pareto(double scale, double shape, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(scale, shape); } @@ -128,7 +128,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Poisson.cs b/src/Numerics/Distributions/Poisson.cs index 87d80219..1e5cd05b 100644 --- a/src/Numerics/Distributions/Poisson.cs +++ b/src/Numerics/Distributions/Poisson.cs @@ -55,7 +55,7 @@ namespace MathNet.Numerics.Distributions /// If is equal or less then 0.0. public Poisson(double lambda) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(lambda); } @@ -67,7 +67,7 @@ namespace MathNet.Numerics.Distributions /// If is equal or less then 0.0. public Poisson(double lambda, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(lambda); } @@ -122,7 +122,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Rayleigh.cs b/src/Numerics/Distributions/Rayleigh.cs index 46ed2459..77087628 100644 --- a/src/Numerics/Distributions/Rayleigh.cs +++ b/src/Numerics/Distributions/Rayleigh.cs @@ -60,7 +60,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Rayleigh(double scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(scale); } @@ -72,7 +72,7 @@ namespace MathNet.Numerics.Distributions /// If is negative. public Rayleigh(double scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(scale); } @@ -115,7 +115,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Stable.cs b/src/Numerics/Distributions/Stable.cs index 096e04b3..816ec63b 100644 --- a/src/Numerics/Distributions/Stable.cs +++ b/src/Numerics/Distributions/Stable.cs @@ -65,7 +65,7 @@ namespace MathNet.Numerics.Distributions /// The location (μ) of the distribution. public Stable(double alpha, double beta, double scale, double location) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(alpha, beta, scale, location); } @@ -79,7 +79,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Stable(double alpha, double beta, double scale, double location, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(alpha, beta, scale, location); } @@ -168,7 +168,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/StudentT.cs b/src/Numerics/Distributions/StudentT.cs index b60c086b..22433370 100644 --- a/src/Numerics/Distributions/StudentT.cs +++ b/src/Numerics/Distributions/StudentT.cs @@ -83,7 +83,7 @@ namespace MathNet.Numerics.Distributions /// The degrees of freedom (ν) for the distribution. Range: ν > 0. public StudentT(double location, double scale, double freedom) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale, freedom); } @@ -98,7 +98,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public StudentT(double location, double scale, double freedom, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(location, scale, freedom); } @@ -175,7 +175,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Weibull.cs b/src/Numerics/Distributions/Weibull.cs index e11a3aab..91578fe0 100644 --- a/src/Numerics/Distributions/Weibull.cs +++ b/src/Numerics/Distributions/Weibull.cs @@ -69,7 +69,7 @@ namespace MathNet.Numerics.Distributions /// The scale (λ) of the Weibull distribution. Range: λ > 0. public Weibull(double shape, double scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, scale); } @@ -81,7 +81,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Weibull(double shape, double scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(shape, scale); } @@ -147,7 +147,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Distributions/Wishart.cs b/src/Numerics/Distributions/Wishart.cs index 719e13be..c850fe5f 100644 --- a/src/Numerics/Distributions/Wishart.cs +++ b/src/Numerics/Distributions/Wishart.cs @@ -74,7 +74,7 @@ namespace MathNet.Numerics.Distributions /// The scale matrix (V) for the Wishart distribution. public Wishart(double degreesOfFreedom, Matrix scale) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(degreesOfFreedom, scale); } @@ -86,7 +86,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Wishart(double degreesOfFreedom, Matrix scale, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(degreesOfFreedom, scale); } diff --git a/src/Numerics/Distributions/Zipf.cs b/src/Numerics/Distributions/Zipf.cs index 61a3a49c..a39a03cd 100644 --- a/src/Numerics/Distributions/Zipf.cs +++ b/src/Numerics/Distributions/Zipf.cs @@ -68,7 +68,7 @@ namespace MathNet.Numerics.Distributions /// The n parameter of the distribution. public Zipf(double s, int n) { - _random = new System.Random(); + _random = new System.Random(Random.RandomSeed.Guid()); SetParameters(s, n); } @@ -80,7 +80,7 @@ namespace MathNet.Numerics.Distributions /// The random number generator which is used to draw random samples. public Zipf(double s, int n, System.Random randomSource) { - _random = randomSource ?? new System.Random(); + _random = randomSource ?? new System.Random(Random.RandomSeed.Guid()); SetParameters(s, n); } @@ -145,7 +145,7 @@ namespace MathNet.Numerics.Distributions public System.Random RandomSource { get { return _random; } - set { _random = value ?? new System.Random(); } + set { _random = value ?? new System.Random(Random.RandomSeed.Guid()); } } /// diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 45978bfe..c29be0c3 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -175,6 +175,7 @@ + diff --git a/src/Numerics/Random/RandomSeed.cs b/src/Numerics/Random/RandomSeed.cs new file mode 100644 index 00000000..cb0dbe79 --- /dev/null +++ b/src/Numerics/Random/RandomSeed.cs @@ -0,0 +1,27 @@ +using System; + +namespace MathNet.Numerics.Random +{ + public static class RandomSeed + { + /// + /// Provides a time-dependent seed value, matching the default behavior of System.Random. + /// WARNING: There is no randomness in this seed and quick repeated calls can cause + /// the same seed value. Do not use for cryptography! + /// + public static int Time() + { + return Environment.TickCount; + } + + /// + /// Provides a seed based on time and unique GUIDs. + /// WARNING: There is only low randomness in this seed, but at least quick repeated + /// calls will result in different seed values. Do not use for cryptography! + /// + public static int Guid() + { + return Environment.TickCount ^ System.Guid.NewGuid().GetHashCode(); + } + } +} diff --git a/src/Numerics/Statistics/MCMC/HybridMC.cs b/src/Numerics/Statistics/MCMC/HybridMC.cs index 1a939f42..0660572a 100644 --- a/src/Numerics/Statistics/MCMC/HybridMC.cs +++ b/src/Numerics/Statistics/MCMC/HybridMC.cs @@ -34,8 +34,6 @@ using MathNet.Numerics.Distributions; namespace MathNet.Numerics.Statistics.Mcmc { - using Random = System.Random; - /// /// A hybrid Monte Carlo sampler for multivariate distributions. /// @@ -86,7 +84,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// The number of iterations in between returning samples. /// When the number of burnInterval iteration is negative. public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval = 0) - : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, new double[x0.Count()], new Random(), Grad) + : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, new double[x0.Count()], new System.Random(Random.RandomSeed.Guid()), Grad) { for (int i = 0; i < _length; i++) { @@ -110,7 +108,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// the components of the momentum. /// When the number of burnInterval iteration is negative. public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv) - : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new Random()) + : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new System.Random(Random.RandomSeed.Guid())) { } @@ -130,7 +128,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// the components of the momentum. /// Random number generator used for sampling the momentum. /// When the number of burnInterval iteration is negative. - public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, Random randomSource) + public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, System.Random randomSource) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, Grad) { } @@ -152,7 +150,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// The method used for numerical differentiation. /// When the number of burnInterval iteration is negative. /// When the length of pSdv is not the same as x0. - public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, Random randomSource, DiffMethod diff) + public HybridMC(double[] x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double[] pSdv, System.Random randomSource, DiffMethod diff) : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff) { _length = x0.Count(); diff --git a/src/Numerics/Statistics/MCMC/MCMCSampler.cs b/src/Numerics/Statistics/MCMC/MCMCSampler.cs index de3e6a01..e9099783 100644 --- a/src/Numerics/Statistics/MCMC/MCMCSampler.cs +++ b/src/Numerics/Statistics/MCMC/MCMCSampler.cs @@ -28,11 +28,11 @@ // OTHER DEALINGS IN THE SOFTWARE. // +using System; + namespace MathNet.Numerics.Statistics.Mcmc { - using System; - - /// + /// /// A method which samples datapoints from a proposal distribution. The implementation of this sampler /// is stateless: no variables are saved between two calls to Sample. This proposal is different from /// in that it doesn't take any parameters; it samples random @@ -85,7 +85,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// /// The random number generator for this class. /// - private Random _randomNumberGenerator; + private System.Random _randomNumberGenerator; /// /// Keeps track of the number of accepted samples. @@ -106,14 +106,14 @@ namespace MathNet.Numerics.Statistics.Mcmc { Accepts = 0; Samples = 0; - RandomSource = new Random(); + RandomSource = new System.Random(Random.RandomSeed.Guid()); } /// /// Gets or sets the random number generator. /// /// When the random number generator is null. - public Random RandomSource + public System.Random RandomSource { get { return _randomNumberGenerator; } set diff --git a/src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs b/src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs index d1f7016b..3b9097b3 100644 --- a/src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs +++ b/src/Numerics/Statistics/MCMC/UnivariateHybridMC.cs @@ -33,8 +33,6 @@ using MathNet.Numerics.Distributions; namespace MathNet.Numerics.Statistics.Mcmc { - using Random = System.Random; - /// /// A hybrid Monte Carlo sampler for univariate distributions. /// @@ -84,7 +82,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// the momentum. /// When the number of burnInterval iteration is negative. public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval = 0, double pSdv = 1) - : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new Random()) + : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, new System.Random(Random.RandomSeed.Guid())) { } @@ -104,7 +102,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// the momentum. /// Random number generator used to sample the momentum. /// When the number of burnInterval iteration is negative. - public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, Random randomSource) + public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, System.Random randomSource) : this(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, pSdv, randomSource, Grad) { } @@ -126,7 +124,7 @@ namespace MathNet.Numerics.Statistics.Mcmc /// The method used for numerical differentiation. /// Random number generator used for sampling the momentum. /// When the number of burnInterval iteration is negative. - public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, Random randomSource, DiffMethod diff) + public UnivariateHybridMC(double x0, DensityLn pdfLnP, int frogLeapSteps, double stepSize, int burnInterval, double pSdv, System.Random randomSource, DiffMethod diff) : base(x0, pdfLnP, frogLeapSteps, stepSize, burnInterval, randomSource, diff) { MomentumStdDev = pSdv;