From 19186a7867fad3775e0c152e232390f4b7b832c8 Mon Sep 17 00:00:00 2001 From: hikari Date: Thu, 9 Dec 2021 11:49:58 +0900 Subject: [PATCH 1/2] fix: Add NextBytes(Span) to RandomSource `RandomSource` does not inherit from `NextBytes (Span )` added in `.NET Core 2.1`, so I added it. https://docs.microsoft.com/dotnet/api/system.random.nextbytes#System_Random_NextBytes_System_Span_System_Byte__ --- src/Numerics/Random/RandomSource.cs | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs index fea612872..0eec22e12 100644 --- a/src/Numerics/Random/RandomSource.cs +++ b/src/Numerics/Random/RandomSource.cs @@ -469,6 +469,27 @@ public sealed override void NextBytes(byte[] buffer) DoSampleBytes(buffer); } + +#if NETCOREAPP2_1_OR_GREATER + /// + /// Fills the elements of a specified array of bytes with random numbers. + /// + /// An array of bytes to contain random numbers. + public sealed override void NextBytes(Span buffer) + { + if (_threadSafe) + { + lock (_lock) + { + DoSampleBytes(buffer); + } + + return; + } + + DoSampleBytes(buffer); + } +#endif /// /// Returns a random number between 0.0 and 1.0. @@ -511,6 +532,18 @@ protected virtual void DoSampleBytes(byte[] buffer) } } +#if NETCOREAPP2_1_OR_GREATER + /// + /// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (). + /// + protected virtual void DoSampleBytes(Span buffer) + { + var temp = new byte[buffer.Length]; + DoSampleBytes(temp); + temp.CopyTo(buffer); + } +#endif + /// /// Returns a random N-bit signed integer greater than or equal to zero and less than 2^N. /// N (bit count) is expected to be greater than zero and less than 32 (not verified). From 3d398facd71e609d9e6552772a4d366e8c170756 Mon Sep 17 00:00:00 2001 From: hikari Date: Thu, 9 Dec 2021 20:06:48 +0900 Subject: [PATCH 2/2] fixup! fix: Add NextBytes(Span) to RandomSource --- src/Numerics/Numerics.csproj | 2 +- src/Numerics/Random/RandomSource.cs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Numerics/Numerics.csproj b/src/Numerics/Numerics.csproj index 1aa53dd2d..facde6f5e 100644 --- a/src/Numerics/Numerics.csproj +++ b/src/Numerics/Numerics.csproj @@ -2,7 +2,7 @@ Library - net5.0;net461;net48;netstandard2.0 + net5.0;netcoreapp2.1;net461;net48;netstandard2.1;netstandard2.0 MathNet.Numerics MathNet.Numerics true diff --git a/src/Numerics/Random/RandomSource.cs b/src/Numerics/Random/RandomSource.cs index 0eec22e12..9e8cddc40 100644 --- a/src/Numerics/Random/RandomSource.cs +++ b/src/Numerics/Random/RandomSource.cs @@ -469,8 +469,8 @@ public sealed override void NextBytes(byte[] buffer) DoSampleBytes(buffer); } - -#if NETCOREAPP2_1_OR_GREATER + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER /// /// Fills the elements of a specified array of bytes with random numbers. /// @@ -532,7 +532,7 @@ protected virtual void DoSampleBytes(byte[] buffer) } } -#if NETCOREAPP2_1_OR_GREATER +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER /// /// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (). ///