Skip to content
Merged
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
43 changes: 37 additions & 6 deletions src/Polyfill/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public static T[] GetItems<T>(

#if !NET6_0_OR_GREATER

[ThreadStatic]
static byte[]? randomNextInt64Buffer;

/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand Down Expand Up @@ -113,11 +116,14 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;

// One buffer per thread rather than one per attempt. It is still filled by NextBytes(byte[]),
// so the values drawn from the generator are the same.
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);

return unchecked((long)(result % range) + minValue);
Expand Down Expand Up @@ -221,6 +227,9 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i

#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER && FeatureMemory

[ThreadStatic]
static byte[]? randomNextBytesBuffer;

/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
Expand All @@ -229,9 +238,31 @@ public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
// A derived type may fill an array in a way of its own, so it gets one array the size of the span
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}

// Random.NextBytes(byte[]) sets each byte to the low 8 bits of the next sample, which is what
// Next() % 256 is. So filling in chunks, and the rest one byte at a time, gives the same bytes
// and leaves the generator in the same state as filling one array the size of the span.
var chunk = randomNextBytesBuffer ??= new byte[256];

while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}

for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif

Expand Down
31 changes: 25 additions & 6 deletions src/Split/net461/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static T[] GetItems<T>(
}
return result;
}
[ThreadStatic]
static byte[]? randomNextInt64Buffer;
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand All @@ -87,11 +89,11 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);
return unchecked((long)(result % range) + minValue);
}
Expand Down Expand Up @@ -168,16 +170,33 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i
}
#endif
#if FeatureMemory
[ThreadStatic]
static byte[]? randomNextBytesBuffer;
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}
var chunk = randomNextBytesBuffer ??= new byte[256];
while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif
/// <summary>
Expand Down
31 changes: 25 additions & 6 deletions src/Split/net462/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static T[] GetItems<T>(
}
return result;
}
[ThreadStatic]
static byte[]? randomNextInt64Buffer;
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand All @@ -87,11 +89,11 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);
return unchecked((long)(result % range) + minValue);
}
Expand Down Expand Up @@ -168,16 +170,33 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i
}
#endif
#if FeatureMemory
[ThreadStatic]
static byte[]? randomNextBytesBuffer;
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}
var chunk = randomNextBytesBuffer ??= new byte[256];
while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif
/// <summary>
Expand Down
31 changes: 25 additions & 6 deletions src/Split/net47/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static T[] GetItems<T>(
}
return result;
}
[ThreadStatic]
static byte[]? randomNextInt64Buffer;
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand All @@ -87,11 +89,11 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);
return unchecked((long)(result % range) + minValue);
}
Expand Down Expand Up @@ -168,16 +170,33 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i
}
#endif
#if FeatureMemory
[ThreadStatic]
static byte[]? randomNextBytesBuffer;
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}
var chunk = randomNextBytesBuffer ??= new byte[256];
while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif
/// <summary>
Expand Down
31 changes: 25 additions & 6 deletions src/Split/net471/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static T[] GetItems<T>(
}
return result;
}
[ThreadStatic]
static byte[]? randomNextInt64Buffer;
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand All @@ -87,11 +89,11 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);
return unchecked((long)(result % range) + minValue);
}
Expand Down Expand Up @@ -168,16 +170,33 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i
}
#endif
#if FeatureMemory
[ThreadStatic]
static byte[]? randomNextBytesBuffer;
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}
var chunk = randomNextBytesBuffer ??= new byte[256];
while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif
/// <summary>
Expand Down
31 changes: 25 additions & 6 deletions src/Split/net472/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static T[] GetItems<T>(
}
return result;
}
[ThreadStatic]
static byte[]? randomNextInt64Buffer;
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
Expand All @@ -87,11 +89,11 @@ public static long NextInt64(this Random target, long minValue, long maxValue)
var range = unchecked((ulong)(maxValue - minValue));
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
var buffer = randomNextInt64Buffer ??= new byte[8];
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
target.NextBytes(buffer);
result = BitConverter.ToUInt64(buffer, 0);
} while (result >= limit);
return unchecked((long)(result % range) + minValue);
}
Expand Down Expand Up @@ -168,16 +170,33 @@ public static string GetString(this Random target, ReadOnlySpan<char> choices, i
}
#endif
#if FeatureMemory
[ThreadStatic]
static byte[]? randomNextBytesBuffer;
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
if (target.GetType() != typeof(Random))
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
return;
}
var chunk = randomNextBytesBuffer ??= new byte[256];
while (buffer.Length >= chunk.Length)
{
target.NextBytes(chunk);
chunk.CopyTo(buffer);
buffer = buffer.Slice(chunk.Length);
}
for (var i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (target.Next() % 256);
}
}
#endif
/// <summary>
Expand Down
Loading
Loading