Created
January 3, 2017 20:10
-
-
Save geraldstanje/658e347f77b58ca7dd774091467594f4 to your computer and use it in GitHub Desktop.
True or false output based on a probability
This file contains 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 ( | |
"time" | |
"fmt" | |
"math/rand" | |
) | |
func Decision(probability float64) bool { | |
// randFloat64() returns a float64, `0.0 <= f < 1.0` | |
return rand.Float64() <= probability | |
} | |
func main() { | |
rand.Seed(time.Now().UTC().UnixNano()) | |
counts := make(map[bool]int) | |
for i:=0; i < 100000; i++ { | |
res := Decision(0.1) | |
counts[res]++ | |
} | |
fmt.Println(counts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment