Add -Shuffle switch to Get-Random command (#11093)

This commit is contained in:
Eugene Samoylov
2020-04-15 10:23:25 -07:00
committed by GitHub
parent 8b3937ecfe
commit 07620b4b79
2 changed files with 48 additions and 18 deletions
@@ -28,6 +28,7 @@ namespace Microsoft.PowerShell.Commands
private const string RandomNumberParameterSet = "RandomNumberParameterSet";
private const string RandomListItemParameterSet = "RandomListItemParameterSet";
private const string ShuffleParameterSet = "ShuffleParameterSet";
private static readonly object[] _nullInArray = new object[] { null };
private enum MyParameterSet
@@ -50,7 +51,8 @@ namespace Microsoft.PowerShell.Commands
{
_effectiveParameterSet = MyParameterSet.RandomListItem;
}
else if (ParameterSetName.Equals(GetRandomCommand.RandomListItemParameterSet, StringComparison.OrdinalIgnoreCase))
else if (ParameterSetName == GetRandomCommand.RandomListItemParameterSet
|| ParameterSetName == GetRandomCommand.ShuffleParameterSet)
{
_effectiveParameterSet = MyParameterSet.RandomListItem;
}
@@ -276,6 +278,7 @@ namespace Microsoft.PowerShell.Commands
/// List from which random elements are chosen.
/// </summary>
[Parameter(ParameterSetName = RandomListItemParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)]
[Parameter(ParameterSetName = ShuffleParameterSet, ValueFromPipeline = true, Position = 0, Mandatory = true)]
[System.Management.Automation.AllowNull]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public object[] InputObject { get; set; }
@@ -283,12 +286,23 @@ namespace Microsoft.PowerShell.Commands
/// <summary>
/// Number of items to output (number of list items or of numbers).
/// </summary>
[Parameter]
[Parameter(ParameterSetName = RandomNumberParameterSet)]
[Parameter(ParameterSetName = RandomListItemParameterSet)]
[ValidateRange(1, int.MaxValue)]
public int Count { get; set; } = 1;
#endregion
#region Shuffle parameter
/// <summary>
/// Gets or sets whether the command should return all input objects in randomized order.
/// </summary>
[Parameter(ParameterSetName = ShuffleParameterSet, Mandatory = true)]
public SwitchParameter Shuffle { get; set; }
#endregion
#region Cmdlet processing methods
private double GetRandomDouble(double minValue, double maxValue)
@@ -492,29 +506,39 @@ namespace Microsoft.PowerShell.Commands
{
if (EffectiveParameterSet == MyParameterSet.RandomListItem)
{
// this allows for $null to be in an array passed to InputObject
foreach (object item in InputObject ?? _nullInArray)
if (ParameterSetName == ShuffleParameterSet)
{
// (3)
if (_numberOfProcessedListItems < Count)
// this allows for $null to be in an array passed to InputObject
foreach (object item in InputObject ?? _nullInArray)
{
Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems");
_chosenListItems.Add(item);
}
else
}
else
{
foreach (object item in InputObject ?? _nullInArray)
{
Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K");
// (1)
if (Generator.Next(_numberOfProcessedListItems + 1) < Count)
// (3)
if (_numberOfProcessedListItems < Count)
{
// (2)
int indexToReplace = Generator.Next(_chosenListItems.Count);
_chosenListItems[indexToReplace] = item;
Debug.Assert(_chosenListItems.Count == _numberOfProcessedListItems, "Initial K elements should all be included in chosenListItems");
_chosenListItems.Add(item);
}
}
else
{
Debug.Assert(_chosenListItems.Count == Count, "After processing K initial elements, the length of chosenItems should stay equal to K");
_numberOfProcessedListItems++;
// (1)
if (Generator.Next(_numberOfProcessedListItems + 1) < Count)
{
// (2)
int indexToReplace = Generator.Next(_chosenListItems.Count);
_chosenListItems[indexToReplace] = item;
}
}
_numberOfProcessedListItems++;
}
}
}
}