Skip to content

Commit

Permalink
fix: Panics when stdDev < 0
Browse files Browse the repository at this point in the history
  • Loading branch information
sp301415 committed May 29, 2024
1 parent 46b9425 commit 2d6c35c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions math/csprng/gaussian_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,22 @@ func (s GaussianSampler[T]) normFloat64() float64 {
}

// Sample returns a number sampled from rounded gaussian distribution
// with standard deviation |stdDev|.
// with standard deviation stdDev.
//
// Panics when stdDev < 0.
func (s GaussianSampler[T]) Sample(stdDev float64) T {
if stdDev < 0 {
panic("negative standard deviation")
}

u := s.normFloat64() * stdDev
return T(int64(math.Round(u)))
}

// SampleSliceAssign samples rounded gaussian values
// with standard deviation |stdDev|, and writes it to vOut.
// with standard deviation stdDev, and writes it to vOut.
//
// Panics when stdDev < 0.
func (s GaussianSampler[T]) SampleSliceAssign(stdDev float64, vOut []T) {
for i := range vOut {
vOut[i] = s.Sample(stdDev)
Expand All @@ -86,6 +94,8 @@ func (s GaussianSampler[T]) SampleSliceAssign(stdDev float64, vOut []T) {

// SampleSliceAddAssign samples rounded gaussian values
// with standard deviation |stdDev|, and adds to vOut.
//
// Panics when stdDev < 0.
func (s GaussianSampler[T]) SampleSliceAddAssign(stdDev float64, vOut []T) {
for i := range vOut {
vOut[i] += s.Sample(stdDev)
Expand Down

0 comments on commit 2d6c35c

Please sign in to comment.