Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % cat main.go
- package main
- import "flag"
- import "fmt"
- import "math"
- import "rand"
- import "time"
- var numprocs *int = flag.Int("procs", 4, "number of processors")
- var iterations *int = flag.Int("iter", int(1e6), "number of iterations per proc")
- func pi(max_iterations int, r chan float64) {
- successes := 0
- var x float64
- var y float64
- for i := 0; i <= max_iterations; i++ {
- x = rand.Float64()
- y = rand.Float64()
- if math.Sqrt(x*x + y*y) <= 1.0 {
- successes++
- }
- }
- fmt.Println(successes, max_iterations)
- fraction := float64(successes) / float64(max_iterations)
- r <- fraction * 4
- }
- func main() {
- flag.Parse()
- rand.Seed(time.Nanoseconds())
- fmt.Println(*numprocs)
- pcount := *numprocs
- iteration_count := *iterations
- r := make(chan float64, pcount)
- for i := 0; i < pcount; i++ {
- go pi(iteration_count, r)
- }
- var total float64 = 0.0
- for i := 0; i < pcount; i++ {
- result := <-r
- total += result
- }
- fmt.Println(total / float64(pcount))
- }
Add Comment
Please, Sign In to add comment