PowerShell

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'

One thought on “PowerShell: Assigning Two Unique Random Numbers

Leave a Reply

Your email address will not be published. Required fields are marked *