Add -Shuffle
switch to Get-Random
command (#11093)
This commit is contained in:
parent
8b3937ecfe
commit
07620b4b79
@ -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)
|
||||
@ -491,8 +505,17 @@ namespace Microsoft.PowerShell.Commands
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (EffectiveParameterSet == MyParameterSet.RandomListItem)
|
||||
{
|
||||
if (ParameterSetName == ShuffleParameterSet)
|
||||
{
|
||||
// this allows for $null to be in an array passed to InputObject
|
||||
foreach (object item in InputObject ?? _nullInArray)
|
||||
{
|
||||
_chosenListItems.Add(item);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (object item in InputObject ?? _nullInArray)
|
||||
{
|
||||
// (3)
|
||||
@ -518,6 +541,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method implements the EndProcessing method for get-random command.
|
||||
|
@ -156,6 +156,12 @@ Describe "Get-Random" -Tags "CI" {
|
||||
$randomNumber[6] | Should -BeNullOrEmpty
|
||||
}
|
||||
|
||||
It "Should return all the numbers for array of 1,2,3,5,8,13 in randomized order when the Shuffle switch is used" {
|
||||
$randomNumber = Get-Random -InputObject 1, 2, 3, 5, 8, 13 -Shuffle
|
||||
$randomNumber.Count | Should -Be 6
|
||||
$randomNumber | Should -BeIn 1, 2, 3, 5, 8, 13
|
||||
}
|
||||
|
||||
It "Should return for a string collection " {
|
||||
$randomNumber = Get-Random -InputObject "red", "yellow", "blue"
|
||||
$randomNumber | Should -Be ("red" -or "yellow" -or "blue")
|
||||
@ -173,7 +179,7 @@ Describe "Get-Random" -Tags "CI" {
|
||||
$firstRandomNumber | Should -Not -Be $secondRandomNumber
|
||||
}
|
||||
|
||||
It "Should return the same number for hexadecimal number and regular number when the switch SetSeed it used " {
|
||||
It "Should return the same number for hexadecimal number and regular number when the switch SetSeed is used " {
|
||||
$firstRandomNumber = Get-Random 0x07FFFFFFFF -SetSeed 20
|
||||
$secondRandomNumber = Get-Random 34359738367 -SetSeed 20
|
||||
$firstRandomNumber | Should -Be @secondRandomNumber
|
||||
|
Loading…
Reference in New Issue
Block a user