diff --git a/apiCount.include.md b/apiCount.include.md index 51cf1a99..875b93c4 100644 --- a/apiCount.include.md +++ b/apiCount.include.md @@ -1 +1 @@ -**API count: 297** \ No newline at end of file +**API count: 299** \ No newline at end of file diff --git a/api_list.include.md b/api_list.include.md index 7f86a59d..13082f37 100644 --- a/api_list.include.md +++ b/api_list.include.md @@ -196,6 +196,8 @@ #### Random * `Void NextBytes(Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))) + * `Void Shuffle(T[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))) + * `Void Shuffle(Span)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))) #### ReadOnlySpan diff --git a/src/Consume/Consume.cs b/src/Consume/Consume.cs index 99df7ce2..835c798f 100644 --- a/src/Consume/Consume.cs +++ b/src/Consume/Consume.cs @@ -460,16 +460,26 @@ void GetMemberWithSameMetadataDefinitionAs(MemberInfo info) #if FeatureMemory void RandomNextBytesSpan() { - // Create a new instance of Random var random = new Random(); - - // Create a span of bytes Span buffer = new byte[10]; - - // Fill the span with random bytes random.NextBytes(buffer); } + + void RandomShuffleSpan() + { + var random = new Random(); + Span span = new byte[10]; + random.Shuffle(span); + } #endif + + void RandomShuffle() + { + var random = new Random(); + var buffer = new byte[10]; + random.Shuffle(buffer); + } + public void SortedList() { var list = new SortedList(); diff --git a/src/Polyfill/Polyfill_Random.cs b/src/Polyfill/Polyfill_Random.cs index 048dd7d4..0ffffe57 100644 --- a/src/Polyfill/Polyfill_Random.cs +++ b/src/Polyfill/Polyfill_Random.cs @@ -24,4 +24,67 @@ public static void NextBytes( array.CopyTo(buffer); } #endif + +#if !NET8_0_OR_GREATER + + /// + /// Performs an in-place shuffle of an array. + /// + /// The array to shuffle. + /// The type of array. + /// + /// This method uses to choose values for shuffling. + /// This method is an O(n) operation. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))")] + public static void Shuffle( + this Random target, + T[] values) + { + int n = values.Length; + + for (int i = 0; i < n - 1; i++) + { + int j = target.Next(i, n); + + if (j != i) + { + T temp = values[i]; + values[i] = values[j]; + values[j] = temp; + } + } + } +#if FeatureMemory + + /// + /// Performs an in-place shuffle of a span. + /// + /// The span to shuffle. + /// The type of span. + /// + /// This method uses to choose values for shuffling. + /// This method is an O(n) operation. + /// + [Link("https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))")] + public static void Shuffle( + this Random target, + Span values) + { + int n = values.Length; + + for (int i = 0; i < n - 1; i++) + { + int j = target.Next(i, n); + + if (j != i) + { + T temp = values[i]; + values[i] = values[j]; + values[j] = temp; + } + } + } +#endif +#endif } \ No newline at end of file diff --git a/src/Tests/PolyfillTests_Random.cs b/src/Tests/PolyfillTests_Random.cs index b522465b..eb121079 100644 --- a/src/Tests/PolyfillTests_Random.cs +++ b/src/Tests/PolyfillTests_Random.cs @@ -4,17 +4,29 @@ partial class PolyfillTests [Test] public void RandomNextBytesSpan() { - // Create a new instance of Random var random = new Random(); - - // Create a span of bytes Span buffer = new byte[10]; - - // Fill the span with random bytes random.NextBytes(buffer); + Assert.IsTrue(buffer.ToArray().Any(b => b != 0)); + } - // Assert that the span is filled with random bytes + [Test] + public void RandomShuffleSpan() + { + var random = new Random(); + Span buffer = new byte[10]; + random.NextBytes(buffer); + random.Shuffle(buffer); Assert.IsTrue(buffer.ToArray().Any(b => b != 0)); } #endif + [Test] + public void RandomShuffleArray() + { + var random = new Random(); + var buffer = new byte[10]; + random.NextBytes(buffer); + random.Shuffle(buffer); + Assert.IsTrue(buffer.ToArray().Any(b => b != 0)); + } } \ No newline at end of file