Beta 42

Research and Development


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 cmdlet 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'