Created
September 27, 2019 19:11
-
-
Save hnakamur/72ffaaed7a5572f7e9a430336f74c682 to your computer and use it in GitHub Desktop.
go exponential random distribution example
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 ( | |
crand "crypto/rand" | |
"encoding/binary" | |
"fmt" | |
"time" | |
"golang.org/x/exp/rand" | |
"gonum.org/v1/gonum/stat/distuv" | |
) | |
func main() { | |
dist := distuv.Exponential{Rate: 10, Src: newPCGSource()} | |
for i := 0; i < 10; i++ { | |
fmt.Println(dist.Rand() * 30) | |
} | |
} | |
func newPCGSource() *rand.PCGSource { | |
s := &rand.PCGSource{} | |
s.Seed(randSeed()) | |
return s | |
} | |
func randSeed() uint64 { | |
var b [8]byte | |
_, err := crand.Read(b[:]) | |
if err != nil { | |
return uint64(time.Now().UnixNano()) | |
} | |
return binary.LittleEndian.Uint64(b[:]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment