-
Notifications
You must be signed in to change notification settings - Fork 0
/
float32.go
46 lines (37 loc) · 942 Bytes
/
float32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package chance
import (
"math"
"math/rand"
)
// Float32Option is a type
type Float32Option func(*Float32Options)
// Float32Options is float32 options
type Float32Options struct {
min float32
max float32
}
func (ch *chance) Float32(options ...Float32Option) float32 {
ops := Float32Options{min: -math.MaxFloat32, max: math.MaxFloat32}
for i := range options {
options[i](&ops)
}
ch.r.Seed(ch.seed)
// TODO: handle error on bad options
return ops.min + rand.Float32()*(ops.max-ops.min)
}
// Float32 returns a random float32
func Float32(options ...Float32Option) float32 {
return defaultChance.Float32(options...)
}
// SetFloat32Min sets min of random float32
func SetFloat32Min(min float32) Float32Option {
return func(iOpts *Float32Options) {
iOpts.min = min
}
}
// SetFloat32Max sets max of random float32
func SetFloat32Max(max float32) Float32Option {
return func(iOpts *Float32Options) {
iOpts.max = max
}
}