| 1 |
package chart |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"math/rand" |
| 6 |
"time" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
_ Sequence = (*RandomSeq)(nil) |
| 11 |
) |
| 12 |
|
| 13 |
|
| 14 |
func RandomValues(count int) []float64 { |
| 15 |
return Seq{NewRandomSequence().WithLen(count)}.Values() |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
func RandomValuesWithMax(count int, max float64) []float64 { |
| 20 |
return Seq{NewRandomSequence().WithMax(max).WithLen(count)}.Values() |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func NewRandomSequence() *RandomSeq { |
| 25 |
return &RandomSeq{ |
| 26 |
rnd: rand.New(rand.NewSource(time.Now().Unix())), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
type RandomSeq struct { |
| 32 |
rnd *rand.Rand |
| 33 |
max *float64 |
| 34 |
min *float64 |
| 35 |
len *int |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (r *RandomSeq) Len() int { |
| 40 |
if r.len != nil { |
| 41 |
return *r.len |
| 42 |
} |
| 43 |
return math.MaxInt32 |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func (r *RandomSeq) GetValue(_ int) float64 { |
| 48 |
if r.min != nil && r.max != nil { |
| 49 |
var delta float64 |
| 50 |
|
| 51 |
if *r.max > *r.min { |
| 52 |
delta = *r.max - *r.min |
| 53 |
} else { |
| 54 |
delta = *r.min - *r.max |
| 55 |
} |
| 56 |
|
| 57 |
return *r.min + (r.rnd.Float64() * delta) |
| 58 |
} else if r.max != nil { |
| 59 |
return r.rnd.Float64() * *r.max |
| 60 |
} else if r.min != nil { |
| 61 |
return *r.min + (r.rnd.Float64()) |
| 62 |
} |
| 63 |
return r.rnd.Float64() |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
func (r *RandomSeq) WithLen(length int) *RandomSeq { |
| 68 |
r.len = &length |
| 69 |
return r |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
func (r RandomSeq) Min() *float64 { |
| 74 |
return r.min |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func (r *RandomSeq) WithMin(min float64) *RandomSeq { |
| 79 |
r.min = &min |
| 80 |
return r |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
func (r RandomSeq) Max() *float64 { |
| 85 |
return r.max |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
func (r *RandomSeq) WithMax(max float64) *RandomSeq { |
| 90 |
r.max = &max |
| 91 |
return r |
| 92 |
} |