Skip to content

Instantly share code, notes, and snippets.

@computerphysicslab
Last active July 30, 2020 12:18
Show Gist options
  • Save computerphysicslab/5766c2b7df97f9918922e5533070babf to your computer and use it in GitHub Desktop.
Save computerphysicslab/5766c2b7df97f9918922e5533070babf to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var wg sync.WaitGroup
func randomWalk1D(n int, c chan float64) {
var x float64
x = 0.0
// fmt.Println(time.Now().UTC())
for i := 0; i < n; i++ {
x += rand.Float64() - 0.5
}
// fmt.Println(time.Now().UTC())
c <- x
defer wg.Done()
}
func main() {
rand.Seed(time.Now().UTC().UnixNano())
numThreads := 18
numSteps := 1000000
c := make(chan float64, numThreads)
wg.Add(numThreads)
for i := 0; i < numThreads; i++ {
go randomWalk1D(numSteps, c)
}
wg.Wait()
close(c)
for i := 0; i < numThreads; i++ {
fmt.Println(<-c)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment