Skip to content

Add a -Shuffle switch to Get-Random for convenient collection shuffling #11022

Closed

Description

Summary of the new feature/enhancement

Get-Random currently requires you to specify a -Count argument in order to extract a random subset from an input collection.

Sometimes you want to shuffle an input collection, i.e. to return all input objects, in randomized order, and it would be nice to be able to express that intent directly, without having to provide a -Count (which may not even be known; you can work around that with -Count ([int]::MaxValue), but that is awkward).

To that end, a new -Shuffle parameter could be introduced:

# WISHFUL THINKING - sample output.
PS> 1..5 | Get-Random -Shuffle
4
2
5
3
1

Activity

  1. added
    Issue-Enhancement
    on Nov 8, 2019
  2. added
    Up-for-Grabs
    on Nov 9, 2019
  3. eugenesmlv commented on Nov 10, 2019

    @eugenesmlv
    Contributor

    I'm working on this one.

  4. eugenesmlv commented on Nov 13, 2019

    @eugenesmlv
    Contributor

    What should happen if one had set both -Count and -Shuffle parameters? Should the command return just random objects and ignore the -Shuffle switch or return the entire collection, in randomized order, despite the -Count? Another possible way is to throw an error, but I don't think it is a good solution.

  5. vexx32 commented on Nov 13, 2019

    @vexx32
    Collaborator

    I think the most elegant solution would simply be to put the -Shuffle parameter in a completely different parameter set, so that it can't be used with -Count, via the Parameter attribute:

    [Parameter(Mandatory = true, ParameterSetName = "ShuffleSet")]
    public SwitchParameter Shuffle { get; set; }

    Then when determining your code path you can use switch (ParameterSetName) to determine which code path to take. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions