From 9e02cd72a5aee5f80e46d9ecfd64f2f8cdae96e7 Mon Sep 17 00:00:00 2001 From: Diego Colombo Date: Wed, 1 Nov 2023 13:05:15 +0000 Subject: [PATCH 1/2] support for random order --- .../SamplingTest.cs | 26 +++++++++++++++++++ .../Sampling.cs | 21 +++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs create mode 100644 src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs diff --git a/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs b/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs new file mode 100644 index 0000000000..618ba503b8 --- /dev/null +++ b/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs @@ -0,0 +1,26 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using FluentAssertions; +using Xunit; + +namespace Microsoft.DotNet.Interactive.AIUtilities.Tests; + +public class SamplingTest +{ + [Fact] + public void can_shuffle_collection() + { + var src = Enumerable.Range(0, 10); + var shuffled = src.RandomOrder().ToArray(); + shuffled.Should().NotBeInAscendingOrder(); + } + + [Fact] + public void does_not_duplicate_items() + { + var src = Enumerable.Range(0, 10).ToArray(); + var shuffled = src.RandomOrder().Distinct().ToArray(); + shuffled.Should().HaveCount(src.Length); + } +} \ No newline at end of file diff --git a/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs b/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs new file mode 100644 index 0000000000..f9288a8079 --- /dev/null +++ b/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs @@ -0,0 +1,21 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.DotNet.Interactive.AIUtilities; + +public static class Sampling +{ + public static IEnumerable RandomOrder(this IEnumerable source) + { + var rnd = new Random(); + var list = new List(source); + while (list.Count > 0) + { + var pos = rnd.Next(list.Count); + var sample = list.ElementAt(pos); + list.RemoveAt(pos); + yield return sample; + + } + } +} \ No newline at end of file From fb1bcf1c3b9d9a7f650cbeb1bfb405f94bc634d8 Mon Sep 17 00:00:00 2001 From: Diego Colombo Date: Wed, 1 Nov 2023 13:12:02 +0000 Subject: [PATCH 2/2] rename to shuffle --- .../SamplingTest.cs | 4 ++-- src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs b/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs index 618ba503b8..8e629b6d76 100644 --- a/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs +++ b/src/Microsoft.DotNet.Interactive.AIUtilities.Tests/SamplingTest.cs @@ -12,7 +12,7 @@ public class SamplingTest public void can_shuffle_collection() { var src = Enumerable.Range(0, 10); - var shuffled = src.RandomOrder().ToArray(); + var shuffled = src.Shuffle().ToArray(); shuffled.Should().NotBeInAscendingOrder(); } @@ -20,7 +20,7 @@ public void can_shuffle_collection() public void does_not_duplicate_items() { var src = Enumerable.Range(0, 10).ToArray(); - var shuffled = src.RandomOrder().Distinct().ToArray(); + var shuffled = src.Shuffle().Distinct().ToArray(); shuffled.Should().HaveCount(src.Length); } } \ No newline at end of file diff --git a/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs b/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs index f9288a8079..ae1fb4479c 100644 --- a/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs +++ b/src/Microsoft.DotNet.Interactive.AIUtilities/Sampling.cs @@ -5,7 +5,7 @@ namespace Microsoft.DotNet.Interactive.AIUtilities; public static class Sampling { - public static IEnumerable RandomOrder(this IEnumerable source) + public static IEnumerable Shuffle(this IEnumerable source) { var rnd = new Random(); var list = new List(source);