简体   繁体   English

随机化Powershell数组中的元素列表

[英]Randomize a List of Elements in a Powershell Array

I'm looking to take an array of elements and return a randomized version of the array in a function. 我正在寻找一个元素数组并在函数中返回一个随机版本的数组。

For example: 例如:

function Randomize-List
{
   Param(
     [int[]]$InputList
   )
   ...code...
   return 10,7,2,6,5,3,1,9,8,4
}

$a = 1..10
Write-Output (Randomize-List -InputList $a)
10
7
2
...

You get the idea. 你明白了。 No idea of how to approach this, I'm new to Powershell, coming from a Python background. 不知道如何处理这个问题,我是Powershell的新手,来自Python背景。

You can use Get-Random to do this in PowerShell. 您可以使用Get-Random在PowerShell中执行此操作。

function Randomize-List
{
   Param(
     [array]$InputList
   )

   return $InputList | Get-Random -Count $InputList.Count;
}

$a = 1..10
Write-Output (Randomize-List -InputList $a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM