Skip to content

Commit

Permalink
Updated multinomial to actually be a method of the Binomial generator
Browse files Browse the repository at this point in the history
  • Loading branch information
chewxy committed Dec 8, 2017
1 parent bff1a8c commit cc201d9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions multinomial.go
Expand Up @@ -5,8 +5,7 @@ import (
"time"
)

// Multinomial returns a slice of ints drawn from the probabilities provided.
func Multinomial(n int, probs []float64, retSize int) []int {
func (g BinomialGenerator) Multinomial(n int, probs []float64, retSize int) []int {
d := len(probs)
if Kahan(probs) > (1.0 + 1e-12) {
panic("Probabilities add up to greater than 1!")
Expand All @@ -16,10 +15,6 @@ func Multinomial(n int, probs []float64, retSize int) []int {
panic("The size of the return vector has to be wholely divisible by the size of the probabilities")
}

g := &BinomialGenerator{
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}

retVal := make([]int, retSize)
for i := 0; i < len(retVal); {
sum := 1.0
Expand All @@ -38,4 +33,13 @@ func Multinomial(n int, probs []float64, retSize int) []int {
i += d
}
return retVal

}

// Multinomial returns a slice of ints drawn from the probabilities provided.
func Multinomial(n int, probs []float64, retSize int) []int {
g := &BinomialGenerator{
Rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
return g.Multinomial(n, probs, retSize)
}

0 comments on commit cc201d9

Please sign in to comment.