Skip to content

Instantly share code, notes, and snippets.

@geraldstanje
Created January 3, 2017 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geraldstanje/658e347f77b58ca7dd774091467594f4 to your computer and use it in GitHub Desktop.
Save geraldstanje/658e347f77b58ca7dd774091467594f4 to your computer and use it in GitHub Desktop.
True or false output based on a probability
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