Skip to content

Instantly share code, notes, and snippets.

@Tsumuri-u
Created September 11, 2024 00:26
Show Gist options
  • Select an option

  • Save Tsumuri-u/f17ac943b1a7e8475de147dd2a23881e to your computer and use it in GitHub Desktop.

Select an option

Save Tsumuri-u/f17ac943b1a7e8475de147dd2a23881e to your computer and use it in GitHub Desktop.
Simulating Runs
package main
import (
"fmt"
"math/rand/v2"
"os"
"strconv"
"sync"
)
func main() {
trialsPerSim, err := strconv.Atoi(os.Args[1])
simulations, err := strconv.Atoi(os.Args[2])
chance, err := strconv.Atoi(os.Args[3])
runLen, err := strconv.Atoi(os.Args[4])
successes := 0
if err != nil {
panic(err)
}
var wg sync.WaitGroup
for i := 0; i < simulations; i++ {
wg.Add(1)
go func() {
defer wg.Done()
simulate(trialsPerSim, chance, runLen, &successes)
}()
}
wg.Wait()
fmt.Println(float64(successes) / float64(simulations))
}
func simulate(trialsPerSim int, chance int, runLen int, successes *int) {
streak := 0
for i := 0; i < trialsPerSim; i++ {
trial := rand.IntN(chance)
if trial == (chance - 1) {
streak += 1
} else {
streak = 0
}
if streak >= runLen {
*successes += 1
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment