An interesting question came up about using the Get-Random
cmdlet to return a random number but with a predefined list of numbers to exclude.
Even though Get-Random
will return a random number, you can also specify a list or range of numbers for it to run against.
1
PS C:\> Get-Random 1,2,3,4,5
…will return a random number between 1 and 5.
What if you want to exclude one or more specific numbers? Get-Random
doesn’t have this capability built in but we can still accomplish this with a little extra work.
Let’s say I want to exclude numbers 1 and 3.
Here’s how we’ll do it:
- Assign the entire range (1 to 5) to an
ArrayList
- Assign the numbers we want to exclude (1 and 3) to another
ArrayList
- Loop through the (second)
ArrayList
that contains the excluded numbers, and any that match what’s in the firstArrayList
, remove it from thatArrayList
.
This is accomplished by passing the excluded numbers to theIndexOf()
method of the firstArrayList
object, and invoking it within itsRemoveAt()
method. The example will shed more light. - With the first
ArrayList
now containing all numbers except for the exclusions, use the result withGet-Random
.
Here’s the code (see below for explanation):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Create ArrayList for entire range
[System.Collections.ArrayList]$NumRange = @(1..5)
# Create ArrayList for excluded numbers
[System.Collections.ArrayList]$Exclusions = @(1,3)
# Loop through excluded numbers
foreach ($num in $Exclusions) {
# If excluded number exists in entire range,
if ($NumRange.Contains($num)) {
# remove it from entire range
$NumRange.RemoveAt($NumRange.IndexOf($num))
}
}
Here’s the output:
1
2
3
4
PS C:\> $NumRange
2
4
5
Now, pipe the results to Get-Random:
1
2
PS C:\> $NumRange | Get-Random
4
There you have it, using Get-Random and a range of numbers with exclusions.
Comments powered by Disqus.