Created
September 11, 2024 00:26
-
-
Save Tsumuri-u/f17ac943b1a7e8475de147dd2a23881e to your computer and use it in GitHub Desktop.
Simulating Runs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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