Created
March 26, 2010 14:08
-
-
Save farhaven/344918 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" | |
| 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