Search code examples
powershellrandom

Using System.Random in PowerShell to Retrieve a Random List from an Array


I need a list of generated six-digit numbers

First I generated a list of numbers.

$Numbers += 0..999999  | ForEach-Object {

    if ( ($_ -as [string]).Length -eq 1 ) {
        "00000$_"
    }elseif ( ($_ -as [string]).Length -eq 2 ) {
        "0000$_"
    }elseif ( ($_ -as [string]).Length -eq 3 ) {
        "000$_"
    }elseif ( ($_ -as [string]).Length -eq 4 ) {
        "00$_"
    }elseif ( ($_ -as [string]).Length -eq 5 ) {
        "0$_"
    }else{$_ -as [string]}
    
} 

Next, I shuffle the list.

$RandomNumbers = $Numbers | Sort-Object { Get-Random }

And that's exactly what takes too long for me.

Is there a way to have the numbers sorted faster?

I thought about "System.Random", but I couldn't get it to work.


Solution

  • You can use Get-Random not only to generate random numbers but also to efficiently shuffle a collection:

    $RandomNumbers = Get-Random -InputObject $Numbers -Count $Numbers.Count
    

    This takes less than a second on my machine for one million numbers, whereas your original code took 27 seconds.

    As expected, the $RandomNumbers array only contains unique numbers, which I have verified using ($RandomNumbers | Sort-Object -unique).Count which outputs 1000000.


    If you have PowerShell 7+ available, you can slightly simplify the code by replacing the the -Count parameter with the -Shuffle parameter:

    $RandomNumbers = Get-Random -InputObject $Numbers -Shuffle