Powershell for Super Bowl

I’m sure this has been done before and likely in a more elegant fashion, but I was sitting at my desk preparing for my Super Bowl gathering of friends and wanted a different way to select the numbers for the squares than just picking numbers from a hat. Now don’t get me wrong… hats work great especially for a small set of numbers like we are discussing here (0 – 9)… twice… but since I was sitting at my desk and I’m trying to be more dev minded in my daily tasks I thought I’d give Powershell a shot.

I knew they had added a cmdlet Get-Random to Powershell and that should make it easy, but I also needed to add code to make sure we didn’t ‘randomly’ re-select any numbers. Easy enough using a array… so here’s the final product and please keep criticism to a minimum as I’m the stereo-typical IT person that enjoys writing code, but I am far from a developer. J I do invite suggestions for making it better/different as I firmly believe the more ways you know how to accomplish a task the better off you’ll be.

# Originally written by Brian Gregor for the purpose of filling out a 
# Super Bowl squares sheet of 0-9. The get-random cmdlet will not 
# select the max value so for 0-9 you must enter 0 as min and 10 as max

# array to represent numbers already selected
$returned = @()

$min = Read-Host 'Minimum number in range'
$max = Read-Host 'Maximum number in range'

Read-Host 'Press <enter> to retrieve first random number'

# Loop and continue to get numbers checking that they have not
# been retrieved previously
    $number = Get-Random -Minimum $min -Maximum $max
    if(($returned -eq 0) -or (!$returned.Contains($number)))
        Write-Host $number
        $returned = $returned + $number        

        # if all numbers between min/max have been retrieved, then set exit $response
        if($returned.Count -ne ($max - $min))
            $response = Read-Host "Retrieve another number? (Y/N)"        
            $response = "N"
until($response.ToUpper() -eq "N")

Write-Host 'Numbers retrieved: '
foreach($number in $returned)
    Write-Host $number

This script can be downloaded from here.