Sunday, April 5, 2015

The futility of the lottery demonstrated with PowerShell

I wrote this a long time ago.  While we often hear how the odds are stacked against those who play lottery, this script will make you get a real feel for it.  When you run this you'll select your Powerball numbers and watch as your (not real) money flies out the window with live winnings/losses/#picks/etc.  I've let this run for hundreds of thousands of simulated picks and have never gotten more than the $100 prize.


001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
$PlayPowerBall = {

Function Get-Five(){ $Result = @()
 While($Result.count -ne 5){
    for($i=0;$i -le 5$i++){
        $Result += (Get-Random -Minimum 1 -Maximum 59)
    }
    $Result = $Result | select -Unique -First 5 | sort}
$Result}
$Winnings = 0
cls
$WinningsTable = @()

$Numbers = @()

for($i=0;$i -le 4;$i++){$NumTest = 0
    while([int]$NumTest -gt [int]59 -or [int]$NumTest -lt [int]1 -or $Numbers -contains $NumTest){
    $NumTest = Read-Host "Please enter lottery number $($i+1) from 1 to 59"
    if([int]$NumTest -gt [int]59 -or [int]$NumTest -lt [int]0){Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. Enter a number between 1 and 59!"}
    if($Numbers -contains $NumTest){Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. No duplicate values allowed!"}
    }
$Numbers += $NumTest
}
$Numbers = $Numbers | sort{[int]$_}
$PB = 0
While([int]$PB -lt [int]1 -or [int]$PB -gt [int]35){
$PB = Read-Host "Please enter the powerball number from 1 to 35"
if([int]$PB -lt [int]1 -or [int]$PB -gt [int]35){
Write-Host -ForegroundColor Red -BackgroundColor Black "Invalid input. Enter a number between 1 and 35!"}
}

$timer = [System.Diagnostics.Stopwatch]::StartNew()
$tries  = 0
$loop = $false
$ConsoleCounter = 0
while($loop -eq $false){
$Result = Get-Five
    $PBResult = Get-Random -Minimum 1 -Maximum 35

[psobject]$row = "" | select WinValue, PickCount, Matches, TimeElapsed

    switch("$((Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result).count),$($PB -eq $PBResult)"){
      
        "10,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 4;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize;$Winnings = $Winnings + 4}
        "8,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 4;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 4}
        "6,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 7;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 7}
        "4,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 7;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 7}
        "4,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 100;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 100}
        "2,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 100;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 100}
        "0,False" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 1000000;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 1000000}
        "0,True" {$WinningsTable += $row;$row.PickCount = $tries;$row.TimeElapsed = $timer.Elapsed.TotalSeconds;$row.WinValue = 160000000;Compare-Object -ReferenceObject $Numbers -DifferenceObject $Result -IncludeEqual -ExcludeDifferent | %{$row.Matches += "$($_.inputobject),"} ;$row | ft -AutoSize$Winnings = $Winnings + 160000000}
        }
      
    $tries++
 Write-Progress -Activity "Running picks against your numbers..." -CurrentOperation "Picks-> $tries Your Numbers: $($Numbers[0])-$($Numbers[1])-$($Numbers[2])-$($Numbers[3])-$($Numbers[4])---$PB) Winnings=`$$Winnings Losses=`$$($tries*2))"
    }
$timer.stop()

}

.$PlayPowerBall