Test Improvement

This commit is contained in:
h z
2024-09-27 22:23:22 +01:00
parent 9126f98173
commit c6843279ce
9 changed files with 75 additions and 20 deletions

View File

@@ -20,7 +20,7 @@ public static class LinqHelper
/// </summary>
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> a)
{
return a.OrderBy(_ => RandomSource.Rnd.NextDouble());
return a.OrderBy(_ => RandomSource.Get());
}
/// <summary>
@@ -62,7 +62,7 @@ public static class LinqHelper
T[] ac = a.ToArray();
int len = ac.Length;
for (int i = 0; i <= count; i++)
yield return ac[RandomSource.Rnd.NextInt64(0, len)];
yield return ac[RandomSource.Rnd.Value.NextInt64(0, len)];
}
/// <summary>
@@ -70,7 +70,7 @@ public static class LinqHelper
/// </summary>
public static IEnumerable<T> Sample<T>(this IEnumerable<T> a, double freq)
{
return a.Where(x => RandomSource.Rnd.NextDouble() < freq);
return a.Where(x => RandomSource.Rnd.Value.NextDouble() < freq);
}
/// <summary>
@@ -559,6 +559,12 @@ public static class LinqHelper
public static CategoryOf<TDim>.FSpecialLieUnitaryMatrix SpLieSum<TDim>
(this IEnumerable<CategoryOf<TDim>.FSpecialLieUnitaryMatrix> es) =>
new(es.LieSum());
public static T WithSeed<T>(this T a, int seed)
{
RandomSource.SetSeed(seed);
if (seed < 0)
RandomSource.Rnd.Value = new Random();
return a;
}
}

View File

@@ -1,3 +1,5 @@
using Skeleton.DataStructure;
namespace Skeleton.Utils.RandomEngines;
/// <summary>
@@ -6,13 +8,21 @@ public static class RandomSource
{
/// <summary>
/// </summary>
public static Random Rnd = new();
public static ThreadLocal<Random> Rnd = new(() => new Random());
public static void SetSeed(int seed)
{
Rnd.Value = new Random(seed);
}
/// <summary>
/// </summary>
/// <returns></returns>
public static double Get()
{
return Rnd.NextDouble();
return Rnd.Value.NextDouble();
}
}