PowerShell: Assigning Two Unique Random Numbers

If you need to get two random numbers from a given numeric range, and you want to make sure they cannot be the same, simply tell Get-Random to pick two numbers, and assign them to two different variables at the same time:

$foreground, $background = Get-Random -InputObject (0..15) -Count 2

In our example, you would draw a random foreground and background color while ensuring that foreground and background color can never be the same:

 
Write-Host -fore $foreground -back $background 'This is random coloring'

1 thought on “PowerShell: Assigning Two Unique Random Numbers

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.