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
7 changes: 7 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,17 @@

#### Random

* `string GetHexString(int, bool)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.gethexstring?view=net-11.0#system-random-gethexstring(system-int32-system-boolean))
* `void GetHexString(Span<char>, bool)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.gethexstring?view=net-11.0#system-random-gethexstring(system-span((system-char))-system-boolean))
* `T[] GetItems<T>(ReadOnlySpan<T>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.getitems?view=net-11.0#system-random-getitems-1(system-readonlyspan((-0))-system-int32))
* `void GetItems<T>(ReadOnlySpan<T>, Span<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.getitems?view=net-11.0#system-random-getitems-1(system-readonlyspan((-0))-system-span((-0))))
* `T[] GetItems<T>(T[], int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.getitems?view=net-11.0#system-random-getitems-1(-0()-system-int32))
* `string GetString(ReadOnlySpan<char>, int)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.getstring?view=net-11.0)
* `void NextBytes(Span<byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=net-11.0#system-random-nextbytes(system-span((system-byte))))
* `long NextInt64(long, long)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64(system-int64-system-int64))
* `long NextInt64(long)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64(system-int64))
* `long NextInt64()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64)
* `float NextSingle()` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextsingle?view=net-11.0)
* `void Shuffle<T>(Span<T>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=net-11.0#system-random-nextbytes(system-span((system-byte))))
* `void Shuffle<T>(T[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes?view=net-11.0#system-random-nextbytes(system-span((system-byte))))
* `Shared` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.shared?view=net-11.0)
Expand Down
12 changes: 12 additions & 0 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,18 @@ void Random_Methods()

var bufferArray = new byte[10];
random.Shuffle(bufferArray);

var nextInt64 = random.NextInt64();
nextInt64 = random.NextInt64(100);
nextInt64 = random.NextInt64(50, 100);
var nextSingle = random.NextSingle();
var hexString = random.GetHexString(10);
hexString = random.GetHexString(10, lowercase: true);
#if FeatureMemory
Span<char> hexDest = stackalloc char[10];
random.GetHexString(hexDest);
var rndString = random.GetString("abc".AsSpan(), 10);
#endif
}

#if FeatureMemory
Expand Down
131 changes: 131 additions & 0 deletions src/Polyfill/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,137 @@ public static T[] GetItems<T>(

#endif

#if !NET6_0_OR_GREATER

/// <summary>
/// Returns a non-negative random integer.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64
public static long NextInt64(this Random target) =>
target.NextInt64(long.MaxValue);

/// <summary>
/// Returns a non-negative random integer that is less than the specified maximum.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64(system-int64)
public static long NextInt64(this Random target, long maxValue) =>
target.NextInt64(0, maxValue);

/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.nextint64?view=net-11.0#system-random-nextint64(system-int64-system-int64)
public static long NextInt64(this Random target, long minValue, long maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(minValue));
}

if (minValue == maxValue)
{
return minValue;
}

var range = (ulong)(maxValue - minValue);
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
} while (result >= limit);

return (long)(result % range) + minValue;
}

/// <summary>
/// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.nextsingle?view=net-11.0
public static float NextSingle(this Random target) =>
(float)target.NextDouble();

#endif

#if !NET10_0_OR_GREATER

/// <summary>
/// Creates a string filled with random hexadecimal characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.gethexstring?view=net-11.0#system-random-gethexstring(system-int32-system-boolean)
public static string GetHexString(this Random target, int stringLength, bool lowercase = false)
{
if (stringLength < 0)
{
throw new ArgumentOutOfRangeException(nameof(stringLength));
}

if (stringLength == 0)
{
return string.Empty;
}

var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
var chars = new char[stringLength];
for (var i = 0; i < stringLength; i++)
{
chars[i] = hexAlphabet[target.Next(16)];
}

return new string(chars);
}

#if FeatureMemory

/// <summary>
/// Fills a buffer with random hexadecimal characters.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.gethexstring?view=net-11.0#system-random-gethexstring(system-span((system-char))-system-boolean)
public static void GetHexString(this Random target, Span<char> destination, bool lowercase = false)
{
var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
for (var i = 0; i < destination.Length; i++)
{
destination[i] = hexAlphabet[target.Next(16)];
}
}

/// <summary>
/// Creates a string populated with characters chosen at random from the provided set of choices.
/// </summary>
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.random.getstring?view=net-11.0
public static string GetString(this Random target, ReadOnlySpan<char> choices, int length)
{
if (choices.IsEmpty)
{
throw new ArgumentException("Choices cannot be empty.", nameof(choices));
}

if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length));
}

if (length == 0)
{
return string.Empty;
}

var chars = new char[length];
for (var i = 0; i < length; i++)
{
chars[i] = choices[target.Next(choices.Length)];
}

return new string(chars);
}

#endif

#endif

#if !NETCOREAPP2_1_OR_GREATER && !NETSTANDARD2_1_OR_GREATER && FeatureMemory

/// <summary>
Expand Down
97 changes: 97 additions & 0 deletions src/Split/net461/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,103 @@ public static T[] GetItems<T>(
}
return result;
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
public static long NextInt64(this Random target) =>
target.NextInt64(long.MaxValue);
/// <summary>
/// Returns a non-negative random integer that is less than the specified maximum.
/// </summary>
public static long NextInt64(this Random target, long maxValue) =>
target.NextInt64(0, maxValue);
/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
public static long NextInt64(this Random target, long minValue, long maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(minValue));
}
if (minValue == maxValue)
{
return minValue;
}
var range = (ulong)(maxValue - minValue);
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
} while (result >= limit);
return (long)(result % range) + minValue;
}
/// <summary>
/// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
/// </summary>
public static float NextSingle(this Random target) =>
(float)target.NextDouble();
/// <summary>
/// Creates a string filled with random hexadecimal characters.
/// </summary>
public static string GetHexString(this Random target, int stringLength, bool lowercase = false)
{
if (stringLength < 0)
{
throw new ArgumentOutOfRangeException(nameof(stringLength));
}
if (stringLength == 0)
{
return string.Empty;
}
var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
var chars = new char[stringLength];
for (var i = 0; i < stringLength; i++)
{
chars[i] = hexAlphabet[target.Next(16)];
}
return new string(chars);
}
#if FeatureMemory
/// <summary>
/// Fills a buffer with random hexadecimal characters.
/// </summary>
public static void GetHexString(this Random target, Span<char> destination, bool lowercase = false)
{
var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
for (var i = 0; i < destination.Length; i++)
{
destination[i] = hexAlphabet[target.Next(16)];
}
}
/// <summary>
/// Creates a string populated with characters chosen at random from the provided set of choices.
/// </summary>
public static string GetString(this Random target, ReadOnlySpan<char> choices, int length)
{
if (choices.IsEmpty)
{
throw new ArgumentException("Choices cannot be empty.", nameof(choices));
}
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
if (length == 0)
{
return string.Empty;
}
var chars = new char[length];
for (var i = 0; i < length; i++)
{
chars[i] = choices[target.Next(choices.Length)];
}
return new string(chars);
}
#endif
#if FeatureMemory
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
Expand Down
97 changes: 97 additions & 0 deletions src/Split/net462/Polyfill_Random.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,103 @@ public static T[] GetItems<T>(
}
return result;
}
/// <summary>
/// Returns a non-negative random integer.
/// </summary>
public static long NextInt64(this Random target) =>
target.NextInt64(long.MaxValue);
/// <summary>
/// Returns a non-negative random integer that is less than the specified maximum.
/// </summary>
public static long NextInt64(this Random target, long maxValue) =>
target.NextInt64(0, maxValue);
/// <summary>
/// Returns a random integer that is within a specified range.
/// </summary>
public static long NextInt64(this Random target, long minValue, long maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException(nameof(minValue));
}
if (minValue == maxValue)
{
return minValue;
}
var range = (ulong)(maxValue - minValue);
var limit = ulong.MaxValue - ulong.MaxValue % range;
ulong result;
do
{
var buf = new byte[8];
target.NextBytes(buf);
result = BitConverter.ToUInt64(buf, 0);
} while (result >= limit);
return (long)(result % range) + minValue;
}
/// <summary>
/// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
/// </summary>
public static float NextSingle(this Random target) =>
(float)target.NextDouble();
/// <summary>
/// Creates a string filled with random hexadecimal characters.
/// </summary>
public static string GetHexString(this Random target, int stringLength, bool lowercase = false)
{
if (stringLength < 0)
{
throw new ArgumentOutOfRangeException(nameof(stringLength));
}
if (stringLength == 0)
{
return string.Empty;
}
var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
var chars = new char[stringLength];
for (var i = 0; i < stringLength; i++)
{
chars[i] = hexAlphabet[target.Next(16)];
}
return new string(chars);
}
#if FeatureMemory
/// <summary>
/// Fills a buffer with random hexadecimal characters.
/// </summary>
public static void GetHexString(this Random target, Span<char> destination, bool lowercase = false)
{
var hexAlphabet = lowercase ? "0123456789abcdef" : "0123456789ABCDEF";
for (var i = 0; i < destination.Length; i++)
{
destination[i] = hexAlphabet[target.Next(16)];
}
}
/// <summary>
/// Creates a string populated with characters chosen at random from the provided set of choices.
/// </summary>
public static string GetString(this Random target, ReadOnlySpan<char> choices, int length)
{
if (choices.IsEmpty)
{
throw new ArgumentException("Choices cannot be empty.", nameof(choices));
}
if (length < 0)
{
throw new ArgumentOutOfRangeException(nameof(length));
}
if (length == 0)
{
return string.Empty;
}
var chars = new char[length];
for (var i = 0; i < length; i++)
{
chars[i] = choices[target.Next(choices.Length)];
}
return new string(chars);
}
#endif
#if FeatureMemory
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
Expand Down
Loading