Skip to content

Instantly share code, notes, and snippets.

@farhaven
Created March 26, 2010 14:08
Show Gist options
  • Select an option

  • Save farhaven/344918 to your computer and use it in GitHub Desktop.

Select an option

Save farhaven/344918 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "rand"
import "math"
import "flag"
func main() {
var hit int64
var total int64
var entropy int64
var verbose bool
flag.Int64Var(&entropy, "entropy", 10000, "define entropy")
flag.BoolVar(&verbose, "verbose", false, "verbose mode")
flag.Parse()
if verbose {
fmt.Printf("Gathering entropy, please wait...\n")
}
for total = 0; total < entropy; total++ {
var x float = rand.Float()
var y float = rand.Float()
if math.Pow(float64(x), 2) + math.Pow(float64(y), 2) <= 1 {
hit += 1;
}
if verbose {
fmt.Printf("\r%.3f%% %d (%d) / %d", (float(total) / float(entropy)) * 100.0, total, hit, entropy)
}
}
var pi float64 = (float64(hit) / float64(total)) * 4;
if verbose {
fmt.Printf("\napprox. value of pi: %v\n", pi)
}else {
fmt.Printf("%v\n", pi)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment