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

Time:02-06

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.

CodePudding user response:

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

CodePudding user response:

The problem here is not that Get-Random is particularly slow, it's that you're generating way more data than necessary. As jdweng comments:

Only generate the minimum number of random numbers that you need.

If you only need 1000 numbers between 0 and 999999, call Get-Random only 1000 times:

$RandomNumbers = 1..1000 |ForEach-Object { 
  '{0:d6}' -f (Get-Random -Min 0 -Max 1000000)
}

If the numbers have to be distinct, you can use a HashSet<int> or a hashtable to keep track of unique values until you reach the desired amount:

$NumberCount = 1000

$newNumbers = @{}
while($newNumbers.psbase.Count -lt $NumberCount){
  $newNumbers[(Get-Random -Min 0 -Max 1000000)] = $true
}

$RandomNumbers = $newNumbers.psbase.Keys |ForEach-Object ToString d6
  • Related