Skip to content

Commit

Permalink
ziggurats don't need to be members of distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Jun 9, 2019
1 parent d74505e commit 464b785
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
6 changes: 2 additions & 4 deletions expo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "math"
// distribution.
type Exponential struct {
Source
z *Ziggurat
Rate float64
}

Expand All @@ -16,13 +15,12 @@ func NewExponential(src Source, rate float64) Exponential {
return Exponential{
Source: src,
Rate: rate,
z: expoZig,
}
}

// Next generates an exponential variate.
func (e Exponential) Next() float64 {
x := e.z.GenNext(e.Source)
x := expoZig.GenNext(e.Source)
return x / e.Rate
}

Expand All @@ -34,7 +32,7 @@ func expoTail(src Source) float64 {
return expoR - math.Log(Uniform0_1{src}.Next())
}

var expoZig = &Ziggurat{
var expoZig = Ziggurat{
PDF: expoPDF,
Tail: expoTail,
Mirrored: false,
Expand Down
6 changes: 2 additions & 4 deletions normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import "math"
// distribution.
type Normal struct {
Source
z *Ziggurat
Mean, StdDev float64
}

Expand All @@ -17,13 +16,12 @@ func NewNormal(src Source, mean, stddev float64) Normal {
Source: src,
Mean: mean,
StdDev: stddev,
z: normalZig,
}
}

// Next generates a normal variate.
func (n Normal) Next() float64 {
x := n.z.GenNext(n.Source)
x := normalZig.GenNext(n.Source)
return n.Mean + x*n.StdDev
}

Expand All @@ -42,7 +40,7 @@ func normalTail(src Source) float64 {
}
}

var normalZig = &Ziggurat{
var normalZig = Ziggurat{
PDF: normalPDF,
Tail: normalTail,
Mirrored: true,
Expand Down

0 comments on commit 464b785

Please sign in to comment.