Skip to content

Commit

Permalink
[Random] Add thread-safe Shared property
Browse files Browse the repository at this point in the history
  • Loading branch information
hikarin522 committed Dec 9, 2021
1 parent 3d398fa commit f132aa5
Show file tree
Hide file tree
Showing 54 changed files with 347 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public void HasRandomSourceContinuous(IContinuousDistribution distribution)
[Test, TestCaseSource(nameof(DiscreteDistributions))]
public void CanSetRandomSourceDiscrete(IDiscreteDistribution distribution)
{
distribution.RandomSource = MersenneTwister.Default;
distribution.RandomSource = MersenneTwister.Shared;
}

[Test, TestCaseSource(nameof(ContinuousDistributions))]
public void CanSetRandomSourceContinuous(IContinuousDistribution distribution)
{
distribution.RandomSource = MersenneTwister.Default;
distribution.RandomSource = MersenneTwister.Shared;
}

[Test, TestCaseSource(nameof(DiscreteDistributions))]
Expand Down
16 changes: 8 additions & 8 deletions src/Numerics/Combinatorics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public static int[] GeneratePermutation(int n, System.Random randomSource = null
/// <param name="randomSource">The random number generator to use. Optional; the default random source will be used if null.</param>
public static void SelectPermutationInplace<T>(T[] data, System.Random randomSource = null)
{
var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

// Fisher-Yates Shuffling
for (int i = data.Length - 1; i > 0; i--)
Expand All @@ -173,7 +173,7 @@ public static void SelectPermutationInplace<T>(T[] data, System.Random randomSou
/// <param name="randomSource">The random number generator to use. Optional; the default random source will be used if null.</param>
public static IEnumerable<T> SelectPermutation<T>(this IEnumerable<T> data, System.Random randomSource = null)
{
var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;
T[] array = data.ToArray();

// Fisher-Yates Shuffling
Expand All @@ -195,7 +195,7 @@ public static bool[] GenerateCombination(int n, System.Random randomSource = nul
{
if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), "Value must not be negative (zero is ok).");

var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

bool[] mask = new bool[n];
for (int i = 0; i < mask.Length; i++)
Expand All @@ -219,7 +219,7 @@ public static bool[] GenerateCombination(int n, int k, System.Random randomSourc
if (k < 0) throw new ArgumentOutOfRangeException(nameof(k), "Value must not be negative (zero is ok).");
if (k > n) throw new ArgumentOutOfRangeException(nameof(k), $"{"k"} must be smaller than or equal to {"n"}.");

var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

bool[] mask = new bool[n];
if (k*3 < n)
Expand Down Expand Up @@ -286,7 +286,7 @@ public static int[] GenerateCombinationWithRepetition(int n, int k, System.Rando
if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), "Value must not be negative (zero is ok).");
if (k < 0) throw new ArgumentOutOfRangeException(nameof(k), "Value must not be negative (zero is ok).");

var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

int[] mask = new int[n];
for (int i = 0; i < k; i++)
Expand Down Expand Up @@ -334,7 +334,7 @@ public static int[] GenerateVariation(int n, int k, System.Random randomSource =
if (k < 0) throw new ArgumentOutOfRangeException(nameof(k), "Value must not be negative (zero is ok).");
if (k > n) throw new ArgumentOutOfRangeException(nameof(k), $"k must be smaller than or equal to n.");

var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

int[] indices = new int[n];
for (int i = 0; i < indices.Length; i++)
Expand Down Expand Up @@ -364,7 +364,7 @@ public static int[] GenerateVariation(int n, int k, System.Random randomSource =
/// <returns>The chosen variation, in random order.</returns>
public static IEnumerable<T> SelectVariation<T>(this IEnumerable<T> data, int elementsToChoose, System.Random randomSource = null)
{
var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;
T[] array = data.ToArray();

if (elementsToChoose < 0) throw new ArgumentOutOfRangeException(nameof(elementsToChoose), "Value must not be negative (zero is ok).");
Expand All @@ -391,7 +391,7 @@ public static int[] GenerateVariationWithRepetition(int n, int k, System.Random
if (n < 0) throw new ArgumentOutOfRangeException(nameof(n), "Value must not be negative (zero is ok).");
if (k < 0) throw new ArgumentOutOfRangeException(nameof(k), "Value must not be negative (zero is ok).");

var random = randomSource ?? SystemRandomSource.Default;
var random = randomSource ?? SystemRandomSource.Shared;

int[] ret = new int[k];
random.NextInt32s(ret, 0, n);
Expand Down
12 changes: 6 additions & 6 deletions src/Numerics/Distributions/Bernoulli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Bernoulli(double p)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = SystemRandomSource.Default;
_random = SystemRandomSource.Shared;
_p = p;
}

Expand All @@ -76,7 +76,7 @@ public Bernoulli(double p, System.Random randomSource)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
_p = p;
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public static bool IsValidParameterSet(double p)
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}

/// <summary>
Expand Down Expand Up @@ -417,7 +417,7 @@ public static int Sample(double p)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SampleUnchecked(SystemRandomSource.Default, p);
return SampleUnchecked(SystemRandomSource.Shared, p);
}

/// <summary>
Expand All @@ -432,7 +432,7 @@ public static IEnumerable<int> Samples(double p)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SamplesUnchecked(SystemRandomSource.Default, p);
return SamplesUnchecked(SystemRandomSource.Shared, p);
}

/// <summary>
Expand All @@ -448,7 +448,7 @@ public static void Samples(int[] values, double p)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

SamplesUnchecked(SystemRandomSource.Default, values, p);
SamplesUnchecked(SystemRandomSource.Shared, values, p);
}
}
}
12 changes: 6 additions & 6 deletions src/Numerics/Distributions/Beta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Beta(double a, double b)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = SystemRandomSource.Default;
_random = SystemRandomSource.Shared;
_shapeA = a;
_shapeB = b;
}
Expand All @@ -85,7 +85,7 @@ public Beta(double a, double b, System.Random randomSource)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
_shapeA = a;
_shapeB = b;
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public static bool IsValidParameterSet(double a, double b)
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}

/// <summary>
Expand Down Expand Up @@ -703,7 +703,7 @@ public static double Sample(double a, double b)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SampleUnchecked(SystemRandomSource.Default, a, b);
return SampleUnchecked(SystemRandomSource.Shared, a, b);
}

/// <summary>
Expand All @@ -719,7 +719,7 @@ public static IEnumerable<double> Samples(double a, double b)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SamplesUnchecked(SystemRandomSource.Default, a, b);
return SamplesUnchecked(SystemRandomSource.Shared, a, b);
}

/// <summary>
Expand All @@ -736,7 +736,7 @@ public static void Samples(double[] values, double a, double b)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

SamplesUnchecked(SystemRandomSource.Default, values, a, b);
SamplesUnchecked(SystemRandomSource.Shared, values, a, b);
}
}
}
6 changes: 3 additions & 3 deletions src/Numerics/Distributions/BetaBinomial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public BetaBinomial(int n, double a, double b)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = SystemRandomSource.Default;
_random = SystemRandomSource.Shared;
_n = n;
_a = a;
_b = b;
Expand All @@ -87,7 +87,7 @@ public BetaBinomial(int n, double a, double b, System.Random randomSource)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
_n = n;
_a = a;
_b = b;
Expand Down Expand Up @@ -135,7 +135,7 @@ public static bool IsValidParameterSet(int n, double a, double b, int k)
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}
/// <summary>
/// Gets the mean of the distribution.
Expand Down
12 changes: 6 additions & 6 deletions src/Numerics/Distributions/BetaScaled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public BetaScaled(double a, double b, double location, double scale)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = SystemRandomSource.Default;
_random = SystemRandomSource.Shared;
_shapeA = a;
_shapeB = b;
_location = location;
Expand All @@ -79,7 +79,7 @@ public BetaScaled(double a, double b, double location, double scale, System.Rand
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
_shapeA = a;
_shapeB = b;
_location = location;
Expand Down Expand Up @@ -176,7 +176,7 @@ public static bool IsValidParameterSet(double a, double b, double location, doub
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}

/// <summary>
Expand Down Expand Up @@ -553,7 +553,7 @@ public static double Sample(double a, double b, double location, double scale)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SampleUnchecked(SystemRandomSource.Default, a, b, location, scale);
return SampleUnchecked(SystemRandomSource.Shared, a, b, location, scale);
}

/// <summary>
Expand All @@ -571,7 +571,7 @@ public static IEnumerable<double> Samples(double a, double b, double location, d
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SamplesUnchecked(SystemRandomSource.Default, a, b, location, scale);
return SamplesUnchecked(SystemRandomSource.Shared, a, b, location, scale);
}

/// <summary>
Expand All @@ -590,7 +590,7 @@ public static void Samples(double[] values, double a, double b, double location,
throw new ArgumentException("Invalid parametrization for the distribution.");
}

SamplesUnchecked(SystemRandomSource.Default, values, a, b, location, scale);
SamplesUnchecked(SystemRandomSource.Shared, values, a, b, location, scale);
}
}
}
12 changes: 6 additions & 6 deletions src/Numerics/Distributions/Binomial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public Binomial(double p, int n)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = SystemRandomSource.Default;
_random = SystemRandomSource.Shared;
_p = p;
_trials = n;
}
Expand All @@ -83,7 +83,7 @@ public Binomial(double p, int n, System.Random randomSource)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
_p = p;
_trials = n;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public static bool IsValidParameterSet(double p, int n)
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}

/// <summary>
Expand Down Expand Up @@ -487,7 +487,7 @@ public static int Sample(double p, int n)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SampleUnchecked(SystemRandomSource.Default, p, n);
return SampleUnchecked(SystemRandomSource.Shared, p, n);
}

/// <summary>
Expand All @@ -503,7 +503,7 @@ public static IEnumerable<int> Samples(double p, int n)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

return SamplesUnchecked(SystemRandomSource.Default, p, n);
return SamplesUnchecked(SystemRandomSource.Shared, p, n);
}

/// <summary>
Expand All @@ -520,7 +520,7 @@ public static void Samples(int[] values, double p, int n)
throw new ArgumentException("Invalid parametrization for the distribution.");
}

SamplesUnchecked(SystemRandomSource.Default, values, p, n);
SamplesUnchecked(SystemRandomSource.Shared, values, p, n);
}
}
}
4 changes: 2 additions & 2 deletions src/Numerics/Distributions/Burr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Burr(double a, double c, double k, System.Random randomSource = null)
{
throw new ArgumentException("Invalid parametrization for the distribution.");
}
_random = randomSource ?? SystemRandomSource.Default;
_random = randomSource ?? SystemRandomSource.Shared;
A = a;
C = c;
K = k;
Expand Down Expand Up @@ -98,7 +98,7 @@ public static bool IsValidParameterSet(double a, double c, double k)
public System.Random RandomSource
{
get => _random;
set => _random = value ?? SystemRandomSource.Default;
set => _random = value ?? SystemRandomSource.Shared;
}

/// <summary>
Expand Down
Loading

0 comments on commit f132aa5

Please sign in to comment.