Skip to content

Commit

Permalink
Export Random.Src in the mathx package (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristinaleonr authored Nov 21, 2022
1 parent f419442 commit ca564ba
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions mathx/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import (

// Random is a source of random values.
type Random struct {
src *rand.Rand
Src *rand.Rand // Pseudo-random source seeded with a given value.
}

// NewRandom returns a new Random that uses the provided seed to generate
// random values.
func NewRandom(seed int64) Random {
src := rand.New(rand.NewSource(seed))
return Random{src: src}
return Random{Src: src}
}

// GetRandomInt returns a non-negative pseudo-random number in the interval [0, max).
Expand All @@ -23,14 +23,14 @@ func (r *Random) GetRandomInt(max int) int {
if max <= 0 {
return 0
}
return r.src.Intn(max)
return r.Src.Intn(max)
}

// GetExpDistributedInt returns a exponentially distributed number in the interval
// [0, +math.MaxFloat64), rounded to the nearest int. Callers can adjust the rate of the
// function through the rate parameter.
func (r *Random) GetExpDistributedInt(rate float64) int {
f := r.src.ExpFloat64() / rate
f := r.Src.ExpFloat64() / rate
index := int(math.Round(f))
return index
}

0 comments on commit ca564ba

Please sign in to comment.