Last active
July 30, 2020 12:18
-
-
Save computerphysicslab/5766c2b7df97f9918922e5533070babf to your computer and use it in GitHub Desktop.
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" | |
"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