Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add NextBytes(Span<byte>) to RandomSource #885

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Numerics/Numerics.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net5.0;net461;net48;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp2.1;net461;net48;netstandard2.1;netstandard2.0</TargetFrameworks>
<AssemblyName>MathNet.Numerics</AssemblyName>
<RootNamespace>MathNet.Numerics</RootNamespace>
<IsPackable>true</IsPackable>
Expand Down
33 changes: 33 additions & 0 deletions src/Numerics/Random/RandomSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,27 @@ public sealed override void NextBytes(byte[] buffer)
DoSampleBytes(buffer);
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <summary>
/// Fills the elements of a specified array of bytes with random numbers.
/// </summary>
/// <param name="buffer">An array of bytes to contain random numbers.</param>
public sealed override void NextBytes(Span<byte> buffer)
{
if (_threadSafe)
{
lock (_lock)
{
DoSampleBytes(buffer);
}

return;
}

DoSampleBytes(buffer);
}
#endif

/// <summary>
/// Returns a random number between 0.0 and 1.0.
/// </summary>
Expand Down Expand Up @@ -511,6 +532,18 @@ protected virtual void DoSampleBytes(byte[] buffer)
}
}

#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
/// <summary>
/// Fills the elements of a specified array of bytes with random numbers in full range, including zero and 255 (<see cref="F:System.Byte.MaxValue"/>).
/// </summary>
protected virtual void DoSampleBytes(Span<byte> buffer)
{
var temp = new byte[buffer.Length];
DoSampleBytes(temp);
temp.CopyTo(buffer);
}
#endif

/// <summary>
/// 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).
Expand Down