Debian Codegraph @

random_sequence.go

raw symbols SCIP
1 package chart
2
3 import (
4 "math"
5 "math/rand"
6 "time"
7 )
8
9 var (
10 _ Sequence = (*RandomSeq)(nil)
11 )
12
13 // RandomValues returns an array of random values.
14 func RandomValues(count int) []float64 {
15 return Seq{NewRandomSequence().WithLen(count)}.Values()
16 }
17
18 // RandomValuesWithMax returns an array of random values with a given average.
19 func RandomValuesWithMax(count int, max float64) []float64 {
20 return Seq{NewRandomSequence().WithMax(max).WithLen(count)}.Values()
21 }
22
23 // NewRandomSequence creates a new random seq.
24 func NewRandomSequence() *RandomSeq {
25 return &RandomSeq{
26 rnd: rand.New(rand.NewSource(time.Now().Unix())),
27 }
28 }
29
30 // RandomSeq is a random number seq generator.
31 type RandomSeq struct {
32 rnd *rand.Rand
33 max *float64
34 min *float64
35 len *int
36 }
37
38 // Len returns the number of elements that will be generated.
39 func (r *RandomSeq) Len() int {
40 if r.len != nil {
41 return *r.len
42 }
43 return math.MaxInt32
44 }
45
46 // GetValue returns the value.
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 // WithLen sets a maximum len
67 func (r *RandomSeq) WithLen(length int) *RandomSeq {
68 r.len = &length
69 return r
70 }
71
72 // Min returns the minimum value.
73 func (r RandomSeq) Min() *float64 {
74 return r.min
75 }
76
77 // WithMin sets the scale and returns the Random.
78 func (r *RandomSeq) WithMin(min float64) *RandomSeq {
79 r.min = &min
80 return r
81 }
82
83 // Max returns the maximum value.
84 func (r RandomSeq) Max() *float64 {
85 return r.max
86 }
87
88 // WithMax sets the average and returns the Random.
89 func (r *RandomSeq) WithMax(max float64) *RandomSeq {
90 r.max = &max
91 return r
92 }