aboutsummaryrefslogtreecommitdiff
path: root/interval.go
diff options
context:
space:
mode:
Diffstat (limited to 'interval.go')
-rw-r--r--interval.go18
1 files changed, 18 insertions, 0 deletions
diff --git a/interval.go b/interval.go
index 5f44f51..763939a 100644
--- a/interval.go
+++ b/interval.go
@@ -1,5 +1,9 @@
package main
+import (
+ "math/rand"
+)
+
type Interval struct {
Min float64
Max float64
@@ -16,3 +20,17 @@ func (i Interval) Include(x float64) bool {
func (i Interval) Surround(x float64) bool {
return i.Min < x && x < i.Max;
}
+
+func (i Interval) Clamp(x float64) float64 {
+ if x < i.Min {
+ return i.Min;
+ }
+ if x > i.Max {
+ return i.Max;
+ }
+ return x;
+}
+
+func (i Interval) Sample() float64 {
+ return rand.Float64() * i.Size() + i.Min;
+}