Skip to content

Commit

Permalink
Add SetRand to intrange
Browse files Browse the repository at this point in the history
  • Loading branch information
200sc committed Jul 2, 2019
1 parent 8bdf90f commit 2acdd84
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions intrange/constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package intrange

import "math/rand"

// NewConstant returns a range which will always return the input constant
func NewConstant(i int) Range {
return Constant(i)
Expand Down Expand Up @@ -33,3 +35,6 @@ func (c Constant) InRange(i int) bool {
func (c Constant) Percentile(float64) int {
return int(c)
}

// SetRand is NOP on Constant
func (c Constant) SetRand(*rand.Rand) {}
8 changes: 7 additions & 1 deletion intrange/infinite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package intrange

import "math"
import (
"math"
"math/rand"
)

// NewInfinite returns a range which will always return math.MaxInt32 and
// is unchangable.
Expand Down Expand Up @@ -35,3 +38,6 @@ func (inf Infinite) InRange(i int) bool {
func (inf Infinite) Percentile(float64) int {
return math.MaxInt32
}

// SetRand is NOP on Infinite
func (inf Infinite) SetRand(*rand.Rand) {}
5 changes: 5 additions & 0 deletions intrange/linear.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ func (lir linear) Percentile(f float64) int {
}
return lir.Min + int(diff)
}

// SetRand sets the rng for linear randomness calls
func (lir linear) SetRand(rng *rand.Rand) {
lir.rng = rng
}
5 changes: 5 additions & 0 deletions intrange/range.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Package intrange holds distributions that accept and return ints
package intrange

import (
"math/rand"
)

// Range represents the ability
// to poll a struct and return an integer,
// distributed over some range dependant
Expand All @@ -11,4 +15,5 @@ type Range interface {
InRange(int) bool
EnforceRange(int) int
Percentile(float64) int
SetRand(*rand.Rand)
}

0 comments on commit 2acdd84

Please sign in to comment.