comments, rename variables to clarify

This commit is contained in:
Steven Polley 2021-05-20 16:57:57 -06:00
parent adad0973f9
commit a6a06adb69

23
main.go
View File

@ -7,37 +7,46 @@ import (
)
func main() {
// Random seed based on time when program runs
rand.Seed(time.Now().UTC().UnixNano())
var balance, investedPercentage, winningPercentage float64
balance = 100
// Initialize variables
var startingBalance, investedPercentage, winningPercentage float64
startingBalance = 100
investedPercentage = 0.80
winningPercentage = 0.70
discreteCompoundingPeriods := 200
numSimulations := 16
outputChannel := make(chan float64)
// Start simulations
for i := 0; i < numSimulations; i++ {
go simulation(balance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
go simulation(startingBalance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
}
// Wait for simulations to finish and write output
for i := 0; i < numSimulations; i++ {
fmt.Println(<-outputChannel)
}
}
// discrete period, symmetric payoff simulator
func simulation(startingBalance, investedPercentage, winningPercentage float64, discreteCompoundingPeriods int, outputChannel chan float64) {
balance := startingBalance
// Each iteration is one bet
for i := 0; i < discreteCompoundingPeriods; i++ {
investedAmount := balance * investedPercentage
// Each bet is a fixed percentage of the balance
betAmount := balance * investedPercentage
if rand.Float64() <= winningPercentage {
// you win
balance += investedAmount
balance += betAmount
} else {
// you lose
balance -= investedAmount
balance -= betAmount
}
}
// Calculate and report the result on the outputChannel
outputChannel <- (balance / startingBalance)
}