This PowerShell script is a lottery number prediction simulator with a graphical user interface (GUI). Here's a summary of what it does:
-
Lottery Number Simulation:
- The script generates a random set of lottery numbers using a specified range and count.
- It trains a model by simulating lottery draws multiple times, which helps in predicting the most frequent numbers.
-
Model Training:
- The script runs a training phase where it simulates a large number of lottery draws (1,000 by default).
- It counts how often each number appears and stores this information in a model.
-
Lottery Prediction:
- Using the trained model, the script predicts a set of lottery numbers that are most likely to be drawn.
-
GUI Components:
- The GUI includes input fields for setting the number of lottery numbers to predict, the maximum number in the range, and the target number of matches.
- Labels display the predicted numbers, actual drawn numbers, the number of matches, and the total number of iterations.
- ListBoxes are used to log simulation progress and to display saved results.
-
Main Simulation Loop:
- The script compares the predicted numbers with a new set of randomly generated lottery numbers.
- If the predicted and actual numbers match a specified target number of times, the result is saved.
- Each time a match is found and saved, the predicted numbers are regenerated based on the trained model.
-
Stopping Conditions:
- The simulation runs until either 6 results are saved or the user stops the simulation.
- The user can start and stop the simulation using buttons in the GUI.
-
User Interaction:
- The script continuously updates the GUI to show the progress of the simulation, including the current predictions, actual draws, matches, and saved results.


Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Function to generate random lottery numbers
function Get-RandomLotteryNumbers {
param (
[int]$count,
[int]$maxNumber
)
return (1..$maxNumber | Get-Random -Count $count | Sort-Object)
}
# Function to train the model
function Train-Model {
param (
[int]$iterations,
[int]$numberCount,
[int]$maxNumber
)
$model = @{}
for ($i = 1; $i -le $iterations; $i++) {
$numbers = Get-RandomLotteryNumbers -count $numberCount -maxNumber $maxNumber
foreach ($number in $numbers) {
if ($model.ContainsKey($number)) {
$model[$number]++
} else {
$model[$number] = 1
}
}
}
return $model
}
# Function to predict lottery numbers based on the model
function Predict-LotteryNumbers {
param (
$model,
[int]$numberCount,
[int]$maxNumber
)
return $model.GetEnumerator() |
Sort-Object Value -Descending |
Select-Object -First $numberCount |
ForEach-Object { $_.Key } |
Sort-Object
}
# Create the main form
$form = New-Object System.Windows.Forms.Form
$form.Text = "PowerShell ML Lottery Simulator"
$form.Size = New-Object System.Drawing.Size(600,600)
$form.StartPosition = "CenterScreen"
# Create input fields for numberCount, maxNumber, and targetMatches
$labelNumberCount = New-Object System.Windows.Forms.Label
$labelNumberCount.Location = New-Object System.Drawing.Point(10,20)
$labelNumberCount.Size = New-Object System.Drawing.Size(100,20)
$labelNumberCount.Text = "Number Count:"
$form.Controls.Add($labelNumberCount)
$textBoxNumberCount = New-Object System.Windows.Forms.TextBox
$textBoxNumberCount.Location = New-Object System.Drawing.Point(120,20)
$textBoxNumberCount.Size = New-Object System.Drawing.Size(50,20)
$textBoxNumberCount.Text = "6"
$form.Controls.Add($textBoxNumberCount)
$labelMaxNumber = New-Object System.Windows.Forms.Label
$labelMaxNumber.Location = New-Object System.Drawing.Point(190,20)
$labelMaxNumber.Size = New-Object System.Drawing.Size(100,20)
$labelMaxNumber.Text = "Max Number:"
$form.Controls.Add($labelMaxNumber)
$textBoxMaxNumber = New-Object System.Windows.Forms.TextBox
$textBoxMaxNumber.Location = New-Object System.Drawing.Point(300,20)
$textBoxMaxNumber.Size = New-Object System.Drawing.Size(50,20)
$textBoxMaxNumber.Text = "49"
$form.Controls.Add($textBoxMaxNumber)
$labelTargetMatches = New-Object System.Windows.Forms.Label
$labelTargetMatches.Location = New-Object System.Drawing.Point(370,20)
$labelTargetMatches.Size = New-Object System.Drawing.Size(100,20)
$labelTargetMatches.Text = "Target Matches:"
$form.Controls.Add($labelTargetMatches)
$textBoxTargetMatches = New-Object System.Windows.Forms.TextBox
$textBoxTargetMatches.Location = New-Object System.Drawing.Point(480,20)
$textBoxTargetMatches.Size = New-Object System.Drawing.Size(50,20)
$textBoxTargetMatches.Text = "6"
$form.Controls.Add($textBoxTargetMatches)
# Create labels
$labelPredicted = New-Object System.Windows.Forms.Label
$labelPredicted.Location = New-Object System.Drawing.Point(10,50)
$labelPredicted.Size = New-Object System.Drawing.Size(580,20)
$form.Controls.Add($labelPredicted)
$labelActual = New-Object System.Windows.Forms.Label
$labelActual.Location = New-Object System.Drawing.Point(10,80)
$labelActual.Size = New-Object System.Drawing.Size(580,20)
$form.Controls.Add($labelActual)
$labelMatching = New-Object System.Windows.Forms.Label
$labelMatching.Location = New-Object System.Drawing.Point(10,110)
$labelMatching.Size = New-Object System.Drawing.Size(580,20)
$form.Controls.Add($labelMatching)
$labelMatchingNumbers = New-Object System.Windows.Forms.Label
$labelMatchingNumbers.Location = New-Object System.Drawing.Point(10,140)
$labelMatchingNumbers.Size = New-Object System.Drawing.Size(580,20)
$form.Controls.Add($labelMatchingNumbers)
$labelIterations = New-Object System.Windows.Forms.Label
$labelIterations.Location = New-Object System.Drawing.Point(10,170)
$labelIterations.Size = New-Object System.Drawing.Size(580,20)
$form.Controls.Add($labelIterations)
# Create a ListBox for logging
$listBoxLog = New-Object System.Windows.Forms.ListBox
$listBoxLog.Location = New-Object System.Drawing.Point(10,200)
$listBoxLog.Size = New-Object System.Drawing.Size(560,150)
$form.Controls.Add($listBoxLog)
# Create a ListBox for saved results
$listBoxSavedResults = New-Object System.Windows.Forms.ListBox
$listBoxSavedResults.Location = New-Object System.Drawing.Point(10,360)
$listBoxSavedResults.Size = New-Object System.Drawing.Size(560,150)
$form.Controls.Add($listBoxSavedResults)
# Create Start and Stop buttons
$buttonStart = New-Object System.Windows.Forms.Button
$buttonStart.Location = New-Object System.Drawing.Point(10,520)
$buttonStart.Size = New-Object System.Drawing.Size(75,23)
$buttonStart.Text = "Start"
$form.Controls.Add($buttonStart)
$buttonStop = New-Object System.Windows.Forms.Button
$buttonStop.Location = New-Object System.Drawing.Point(95,520)
$buttonStop.Size = New-Object System.Drawing.Size(75,23)
$buttonStop.Text = "Stop"
$buttonStop.Enabled = $false
$form.Controls.Add($buttonStop)
# Create global variables for control
$global:running = $false
$global:savedResults = @()
# Main script logic
$trainingIterations = 1000
$buttonStart.Add_Click({
$global:running = $true
$buttonStart.Enabled = $false
$buttonStop.Enabled = $true
$listBoxLog.Items.Clear()
$listBoxSavedResults.Items.Clear()
$global:savedResults = @()
$iterations = 0
$matchCount = 0
$numberCount = [int]$textBoxNumberCount.Text
$maxNumber = [int]$textBoxMaxNumber.Text
$targetMatches = [int]$textBoxTargetMatches.Text
$model = Train-Model -iterations $trainingIterations -numberCount $numberCount -maxNumber $maxNumber
$listBoxLog.Items.Add("Model trained.")
$prediction = Predict-LotteryNumbers -model $model -numberCount $numberCount -maxNumber $maxNumber
while ($global:running -and $listBoxSavedResults.Items.Count -lt 6) {
$iterations++
$actualNumbers = Get-RandomLotteryNumbers -count $numberCount -maxNumber $maxNumber
$matchingNumbers = Compare-Object $prediction $actualNumbers -IncludeEqual -ExcludeDifferent
$matchCount = $matchingNumbers.Count
$labelPredicted.Text = "Predicted numbers: $($prediction -join ', ')"
$labelActual.Text = "Actual numbers: $($actualNumbers -join ', ')"
$labelMatching.Text = "Matching numbers: $matchCount"
$labelMatchingNumbers.Text = "Matching: $($matchingNumbers.InputObject -join ', ')"
$labelIterations.Text = "Iterations: $iterations"
$listBoxLog.Items.Add("Iteration $iterations - Matches: $matchCount")
$listBoxLog.TopIndex = $listBoxLog.Items.Count - 1
if ($matchCount -ge $targetMatches) {
$result = "Iteration $iterations - Predicted: $($prediction -join ', ') | Actual: $($actualNumbers -join ', ') | Matches: $($matchingNumbers.InputObject -join ', ')"
$listBoxSavedResults.Items.Add($result)
$global:savedResults += $result
# Change the predicted numbers after saving the result
$prediction = Predict-LotteryNumbers -model $model -numberCount $numberCount -maxNumber $maxNumber
# Stop the simulation if 6 results are saved
if ($listBoxSavedResults.Items.Count -ge 6) {
$listBoxLog.Items.Add("Finished! 6 results saved.")
$global:running = $false
}
}
$form.Refresh()
[System.Windows.Forms.Application]::DoEvents()
}
if ($global:running -eq $false -and $listBoxSavedResults.Items.Count -lt 6) {
$listBoxLog.Items.Add("Simulation stopped.")
}
$buttonStart.Enabled = $true
$buttonStop.Enabled = $false
$global:running = $false
})
$buttonStop.Add_Click({
$global:running = $false
$buttonStart.Enabled = $true
$buttonStop.Enabled = $false
})
# Show the form
$form.ShowDialog()