Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created September 27, 2019 19:11
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 hnakamur/72ffaaed7a5572f7e9a430336f74c682 to your computer and use it in GitHub Desktop.
Save hnakamur/72ffaaed7a5572f7e9a430336f74c682 to your computer and use it in GitHub Desktop.
go exponential random distribution example
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