fixing examples

This commit is contained in:
Will Charczuk 2017-05-12 17:06:45 -07:00
parent 4d93883953
commit 50b025e273
32 changed files with 87 additions and 67 deletions

View file

@ -12,8 +12,8 @@ func RandomValues(count int) []float64 {
}
// RandomValuesWithAverage returns an array of random values with a given average.
func RandomValuesWithAverage(average float64, count int) []float64 {
return Seq{NewRandom().WithAverage(average).WithLen(count)}.Array()
func RandomValuesWithMax(count int, max float64) []float64 {
return Seq{NewRandom().WithMax(max).WithLen(count)}.Array()
}
// NewRandom creates a new random seq.
@ -25,10 +25,10 @@ func NewRandom() *Random {
// Random is a random number seq generator.
type Random struct {
rnd *rand.Rand
scale *float64
average *float64
len *int
rnd *rand.Rand
max *float64
min *float64
len *int
}
// Len returns the number of elements that will be generated.
@ -41,10 +41,20 @@ func (r *Random) Len() int {
// GetValue returns the value.
func (r *Random) GetValue(_ int) float64 {
if r.average != nil && r.scale != nil {
return *r.average + *r.scale - (r.rnd.Float64() * (2 * *r.scale))
} else if r.scale != nil {
return r.rnd.Float64() * *r.scale
if r.min != nil && r.max != nil {
var delta float64
if *r.max > *r.min {
delta = *r.max - *r.min
} else {
delta = *r.min - *r.max
}
return *r.min + (r.rnd.Float64() * delta)
} else if r.max != nil {
return r.rnd.Float64() * *r.max
} else if r.min != nil {
return *r.min + (r.rnd.Float64())
}
return r.rnd.Float64()
}
@ -55,24 +65,24 @@ func (r *Random) WithLen(length int) *Random {
return r
}
// Scale returns the scale.
func (r Random) Scale() *float64 {
return r.scale
// Min returns the minimum value.
func (r Random) Min() *float64 {
return r.min
}
// WithScale sets the scale and returns the Random.
func (r *Random) WithScale(scale float64) *Random {
r.scale = &scale
// WithMin sets the scale and returns the Random.
func (r *Random) WithMin(min float64) *Random {
r.min = &min
return r
}
// Average returns the average.
func (r Random) Average() *float64 {
return r.average
// Max returns the maximum value.
func (r Random) Max() *float64 {
return r.max
}
// WithAverage sets the average and returns the Random.
func (r *Random) WithAverage(average float64) *Random {
r.average = &average
// WithMax sets the average and returns the Random.
func (r *Random) WithMax(max float64) *Random {
r.max = &max
return r
}

View file

@ -9,10 +9,12 @@ import (
func TestRandomRegression(t *testing.T) {
assert := assert.New(t)
randomProvider := NewRandom().WithLen(100).WithAverage(256)
assert.Equal(100, randomProvider.Len())
assert.Equal(256, *randomProvider.Average())
randomProvider := NewRandom().WithLen(4096).WithMax(256)
assert.Equal(4096, randomProvider.Len())
assert.Equal(256, *randomProvider.Max())
randomValues := New(randomProvider).Array()
assert.Len(randomValues, 100)
randomSequence := New(randomProvider)
randomValues := randomSequence.Array()
assert.Len(randomValues, 4096)
assert.InDelta(128, randomSequence.Average(), 10.0)
}