Browse Source

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

`RandomSource` does not inherit from `NextBytes (Span <byte>)` 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__
pull/885/head
hikari 5 years ago
committed by GitHub
parent
commit
19186a7867
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      src/Numerics/Random/RandomSource.cs

33
src/Numerics/Random/RandomSource.cs

@ -469,6 +469,27 @@ namespace MathNet.Numerics.Random
DoSampleBytes(buffer); DoSampleBytes(buffer);
} }
#if NETCOREAPP2_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> /// <summary>
/// Returns a random number between 0.0 and 1.0. /// Returns a random number between 0.0 and 1.0.
@ -511,6 +532,18 @@ namespace MathNet.Numerics.Random
} }
} }
#if NETCOREAPP2_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> /// <summary>
/// Returns a random N-bit signed integer greater than or equal to zero and less than 2^N. /// 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). /// N (bit count) is expected to be greater than zero and less than 32 (not verified).

Loading…
Cancel
Save