From aef532dc1dfe1ac39417650dbf12f8790ea0fd83 Mon Sep 17 00:00:00 2001 From: Knight <1256599396@qq.com> Date: Thu, 19 Sep 2024 14:09:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/EasilyNET.Core/Misc/RandomExtensions.cs | 11 ++++--- test/EasilyNET.Core.Benchmark/Program.cs | 30 ++++++++++--------- .../EasilyNET.Test.Unit/Randoms/RandomTest.cs | 26 ++++++++++++++++ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 test/EasilyNET.Test.Unit/Randoms/RandomTest.cs diff --git a/src/EasilyNET.Core/Misc/RandomExtensions.cs b/src/EasilyNET.Core/Misc/RandomExtensions.cs index 00718968..9c2e4422 100644 --- a/src/EasilyNET.Core/Misc/RandomExtensions.cs +++ b/src/EasilyNET.Core/Misc/RandomExtensions.cs @@ -25,12 +25,15 @@ public static class RandomExtensions // ReSharper disable once UnusedParameter.Global public static int StrictNext(this Random rand, int startIndex = 0, int maxValue = int.MaxValue) { - var buffer = new byte[4]; - using (var rng = RandomNumberGenerator.Create()) + if (startIndex > maxValue) { - rng.GetBytes(buffer); + throw new ArgumentOutOfRangeException(nameof(startIndex), "startIndex must be less than maxValue."); } - var randomValue = BitConverter.ToInt32(buffer, 0); + + Span buffer = stackalloc byte[4]; + RandomNumberGenerator.Fill(buffer); + + int randomValue = BitConverter.ToInt32(buffer); return new Random(randomValue).Next(startIndex, maxValue); } diff --git a/test/EasilyNET.Core.Benchmark/Program.cs b/test/EasilyNET.Core.Benchmark/Program.cs index ea5d96a5..ebc9f3ca 100644 --- a/test/EasilyNET.Core.Benchmark/Program.cs +++ b/test/EasilyNET.Core.Benchmark/Program.cs @@ -3,6 +3,7 @@ using BenchmarkDotNet.Diagnosers; using BenchmarkDotNet.Jobs; using BenchmarkDotNet.Running; +using EasilyNET.Core.Misc; namespace EasilyNET.Core.Benchmark; @@ -10,26 +11,27 @@ namespace EasilyNET.Core.Benchmark; /// /// [Config(typeof(Config))] -public class AsyncSemaphoreBenchmark +public class AsyncStrictNextBenchmark { - // private readonly AsyncSemaphore _semaphore = new(1); - // - // private readonly AsyncSemaphoreCas _semaphoreCas = new(); - // + + private readonly Random _random = new Random(); + // [Benchmark] - // public async Task Asynchronously() + // public void TestStrictNext() // { - // var task = _semaphore.WaitAsync(); - // _semaphore.Release(); - // await task; + // for (int i = 0; i < 5000; i++) + // { + // _random.StrictNext(); + // } // } // // [Benchmark] - // public async Task AsynchronouslyCAS() + // public void TestStrictNext2() // { - // var task = _semaphore.WaitAsync(); - // _semaphore.Release(); - // await task; + // for (int i = 0; i < 5000; i++) + // { + // _random.StrictNext2(); + // } // } private class Config : ManualConfig @@ -50,6 +52,6 @@ public static class Program /// public static void Main() { - _ = BenchmarkRunner.Run(); + _ = BenchmarkRunner.Run(); } } \ No newline at end of file diff --git a/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs b/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs new file mode 100644 index 00000000..e5dca668 --- /dev/null +++ b/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs @@ -0,0 +1,26 @@ +using EasilyNET.Core.Misc; + +namespace EasilyNET.Test.Unit.Randoms; +[TestClass] +public class RandomTest +{ + private readonly Random _random = new Random(); + + [TestMethod] + public void StrictNext_ShouldReturnValueWithinRange() + { + // Act + int result = _random.StrictNext(); + + // Assert + Assert.IsTrue(result is >= 0 and < int.MaxValue); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentOutOfRangeException))] + public void StrictNext2_ShouldThrowArgumentOutOfRangeException_WhenStartIndexIsGreaterThanOrEqualToMaxValue() + { + // Act + _random.StrictNext(); + } +} \ No newline at end of file From 4085d70deecf9bd72ff2623a9a7a8efa5d7d41f4 Mon Sep 17 00:00:00 2001 From: Knight <1256599396@qq.com> Date: Thu, 19 Sep 2024 15:06:20 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A1=A5=E5=85=85=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=A1=88=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/EasilyNET.Test.Unit/Randoms/RandomTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs b/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs index e5dca668..df15b8a8 100644 --- a/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs +++ b/test/EasilyNET.Test.Unit/Randoms/RandomTest.cs @@ -21,6 +21,6 @@ public void StrictNext_ShouldReturnValueWithinRange() public void StrictNext2_ShouldThrowArgumentOutOfRangeException_WhenStartIndexIsGreaterThanOrEqualToMaxValue() { // Act - _random.StrictNext(); + _random.StrictNext(10,5); } } \ No newline at end of file From 8105b00b5d71e5d120aecaa730c80cef8ec4b67b Mon Sep 17 00:00:00 2001 From: Joe Du <13188169+joesdu@users.noreply.github.com> Date: Thu, 19 Sep 2024 15:14:27 +0800 Subject: [PATCH 3/3] format code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 删除无用空行,使用var简单类型. --- src/EasilyNET.Core/Misc/RandomExtensions.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/EasilyNET.Core/Misc/RandomExtensions.cs b/src/EasilyNET.Core/Misc/RandomExtensions.cs index 9c2e4422..1a3e0694 100644 --- a/src/EasilyNET.Core/Misc/RandomExtensions.cs +++ b/src/EasilyNET.Core/Misc/RandomExtensions.cs @@ -29,11 +29,9 @@ public static int StrictNext(this Random rand, int startIndex = 0, int maxValue { throw new ArgumentOutOfRangeException(nameof(startIndex), "startIndex must be less than maxValue."); } - Span buffer = stackalloc byte[4]; RandomNumberGenerator.Fill(buffer); - - int randomValue = BitConverter.ToInt32(buffer); + var randomValue = BitConverter.ToInt32(buffer); return new Random(randomValue).Next(startIndex, maxValue); } @@ -51,4 +49,4 @@ public static double NextGauss(this Random rand, double mean, double stdDev) var randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2); return mean + stdDev * randStdNormal; } -} \ No newline at end of file +}