Skip to content

Instantly share code, notes, and snippets.

@hnakamur hnakamur/main.go
Created Sep 27, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.