Documentation
¶
Overview ¶
Package randutil provides a utility function to get non-negative pesudo-random int using math/rand or crypto/rand.
This package also provides Chooser which chooses an item from choices. Each choice has a weight which affects possibility for it to be choosed.
Index ¶
- func Erlang(intner Intner, lambda float64, k int) (float64, error)
- func Exp(intner Intner, lambda float64) (float64, error)
- func Float64(intner Intner) (float64, error)
- func NormFloat64(intner Intner) (float64, error)
- func Poisson(intner Intner, lambda float64) (int, error)
- func RandomSeed() (int64, error)
- type Choice
- type Chooser
- type CubicNoise
- type Encoder
- type General
- type Intner
- func NewCryptoIntner() Intner
- func NewMathIntner(seed int64) Intner
- func NewPCG32Intner(seed uint64) Intner
- func NewPCG64Intner(seedH, seedL uint64) Intner
- func NewXorShift64StarIntner(seed uint64) Intner
- func NewXorShift128PlusIntner(seed uint64) Intner
- func NewXorShift1024StarIntner(seed uint64) Intner
- type Reader
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NormFloat64 ¶
NormFloat64 returns a normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution (mean = 0, stddev = 1). To produce a different normal distribution, callers can adjust the output using:
sample = NormFloat64() * desiredStdDev + desiredMean
func RandomSeed ¶
RandomSeed returns a random seed using the cryptographically secure pseudo random number generator.
Types ¶
type Choice ¶
type Choice struct {
Weight uint
Item interface{}
}
Choice represents an item to be choosed. It has a weight which affects possibility to be choosed.
type Chooser ¶
type Chooser struct {
// contains filtered or unexported fields
}
Chooser represents a chooser that chooses an item randomly.
func NewChooser ¶
NewChooser returns a new chooser for choices. It returns an error if total weight of choices is not positive.
Example ¶
package main
import (
"fmt"
"github.com/hnakamur/randutil"
)
func main() {
c, err := randutil.NewChooser(
randutil.NewCryptoIntner(),
[]randutil.Choice{
// This is a cheated coin that possibility of tail is twice of head.
{Weight: 1, Item: "head"},
{Weight: 2, Item: "tail"},
},
)
if err != nil {
panic(err)
}
counts := map[string]int{
"head": 0,
"tail": 0,
}
for i := 0; i < 10000; i++ {
v, err := c.Choose()
if err != nil {
panic(err)
}
counts[v.(string)]++
}
fmt.Printf("head=%d, tail=%d\n", counts["head"], counts["tail"])
}
Output:
type CubicNoise ¶
type CubicNoise struct {
// contains filtered or unexported fields
}
func NewCubicNoise ¶
func NewCubicNoise(seed, octave, periodX, periodY int32) *CubicNoise
func (*CubicNoise) Sample1D ¶
func (n *CubicNoise) Sample1D(x float64) float64
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder generates random values and encodes them in the specified digit characters.
func NewEncoder ¶
NewEncoder creates an Encoder. The length of digits must be between 1 and 256.
func (*Encoder) RandomBytes ¶
Bytes writes random encoded bytes to the passed buffer.
type General ¶
type General struct {
// contains filtered or unexported fields
}
General is a general discrete distributed random value generator.
func NewGeneral ¶
NewGeneral creates a general discrete distributed random value generator.
The rules for g are: * The lenght of g must be greater than or equal to 2. * The first element of g must be 0. * The last element of g must be 1. * The g must be monotonously increasing, that is, g[i] >= g[i-1] for any i.
type Intner ¶
Intner is the interface that returns a non-negative pseudo random int in the specified range.
Intn returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n <= 0.
Int63n returns, as an int64, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.
func NewCryptoIntner ¶
func NewCryptoIntner() Intner
NewCryptoIntner returns a new Intner using crypto/rand.
Example ¶
package main
import (
"fmt"
"github.com/hnakamur/randutil"
)
func main() {
intner := randutil.NewCryptoIntner()
for i := 0; i < 10; i++ {
v, err := intner.Intn(6)
if err != nil {
panic(err)
}
fmt.Printf("dice was %d\n", v+1)
}
}
Output:
func NewMathIntner ¶
NewMathIntner returns a new Intner using math/rand. This Intner is not safe for concurrent use by multiple goroutines.
Example ¶
package main
import (
"fmt"
"time"
"github.com/hnakamur/randutil"
)
func main() {
intner := randutil.NewMathIntner(time.Now().UnixNano())
for i := 0; i < 10; i++ {
v, err := intner.Intn(6)
if err != nil {
panic(err)
}
fmt.Printf("dice was %d\n", v+1)
}
}
Output:
func NewPCG32Intner ¶
NewPCG32Intner returns a new Intner using pcg.Pcg32.
func NewPCG64Intner ¶
NewPCG64Intner returns a new Intner using pcg.Pcg64.
func NewXorShift64StarIntner ¶
NewXorShift64StarIntner returns a new Intner using xorshift.XorShift64Star.
func NewXorShift128PlusIntner ¶
NewXorShift128PlusIntner returns a new Intner using xorshift.XorShift128Plus.
func NewXorShift1024StarIntner ¶
NewXorShift1024StarIntner returns a new Intner using xorshift.XorShift1024Star.
Example ¶
package main
import (
"fmt"
"time"
"github.com/hnakamur/randutil"
)
func main() {
intner := randutil.NewXorShift1024StarIntner(uint64(time.Now().UnixNano()))
for i := 0; i < 10; i++ {
v, err := intner.Intn(6)
if err != nil {
panic(err)
}
fmt.Printf("dice was %d\n", v+1)
}
}
Output:
