英文:
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.
英文:
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.
答案1
得分: 3
你可以使用 Get-Random
不仅生成随机数,还可以有效地洗牌一个集合:
$RandomNumbers = Get-Random -InputObject $Numbers -Count $Numbers.Count
这在我的计算机上处理一百万个数字不到一秒,而你的原始代码需要27秒。
如预期,$RandomNumbers
数组仅包含唯一的数字,我已使用 ($RandomNumbers | Sort-Object -unique).Count
进行验证,输出结果为 1000000
。
如果你有PowerShell 7+ 可用,你可以通过用 -Shuffle
参数替换 -Count
参数来稍微简化代码:
$RandomNumbers = Get-Random -InputObject $Numbers -Shuffle
英文:
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
答案2
得分: 2
问题不在于Get-Random
特别慢,而在于您生成的数据远远超过了所需。正如jdweng的评论所述:
> 只生成所需数量的随机数。
如果您只需要在0到999999之间的1000个数字,请只调用Get-Random
1000次:
$RandomNumbers = 1..1000 | ForEach-Object {
'{0:d6}' -f (Get-Random -Min 0 -Max 1000000)
}
如果这些数字必须是_不同的_,您可以使用HashSet<int>
或哈希表来跟踪唯一的值,直到达到所需数量:
$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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论