44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"math/rand"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
rand.Seed(time.Now().UTC().UnixNano())
|
||
|
|
||
|
var balance, investedPercentage, winningPercentage float64
|
||
|
balance = 100
|
||
|
investedPercentage = 0.80
|
||
|
winningPercentage = 0.70
|
||
|
discreteCompoundingPeriods := 200
|
||
|
numSimulations := 16
|
||
|
outputChannel := make(chan float64)
|
||
|
|
||
|
for i := 0; i < numSimulations; i++ {
|
||
|
go simulation(balance, investedPercentage, winningPercentage, discreteCompoundingPeriods, outputChannel)
|
||
|
}
|
||
|
|
||
|
for i := 0; i < numSimulations; i++ {
|
||
|
fmt.Println(<-outputChannel)
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func simulation(startingBalance, investedPercentage, winningPercentage float64, discreteCompoundingPeriods int, outputChannel chan float64) {
|
||
|
balance := startingBalance
|
||
|
for i := 0; i < discreteCompoundingPeriods; i++ {
|
||
|
investedAmount := balance * investedPercentage
|
||
|
if rand.Float64() <= winningPercentage {
|
||
|
// you win
|
||
|
balance += investedAmount
|
||
|
} else {
|
||
|
// you lose
|
||
|
balance -= investedAmount
|
||
|
}
|
||
|
}
|
||
|
outputChannel <- (balance / startingBalance)
|
||
|
}
|