-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
110 lines (92 loc) · 2.64 KB
/
util.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"encoding/json"
"fmt"
"github.com/go-chi/chi"
"github.com/gonum/stat/distuv"
distuv2 "gonum.org/v1/gonum/stat/distuv"
"math/rand"
"net/http"
"strings"
)
// HandleErr is a wrapper to panick if the error exists
func HandleErr(err error) {
if err != nil {
panic(err)
}
}
// respondwithError return error message
func respondWithError(w http.ResponseWriter, code int, msg string) {
respondwithJSON(w, code, map[string]string{"message": msg})
}
// respondwithJSON write json response format
func respondwithJSON(w http.ResponseWriter, code int, payload interface{}) {
response, _ := json.Marshal(payload)
fmt.Println(payload)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
// FileServer is the handler for serving files at a static route
func FileServer(r chi.Router, path string, root http.FileSystem) {
if strings.ContainsAny(path, "{}*") {
panic("FileServer does not permit URL parameters.")
}
fs := http.StripPrefix(path, http.FileServer(root))
if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
path += "/"
}
path += "*"
r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fs.ServeHTTP(w, r)
}))
}
func UniformRandMinMax(min float64, max float64) float64 {
rnd := rand.Float64()
return rnd*(max-min) + min
}
// UniformRand randomly picks something from 0 to 1
func UniformRand() float64 {
max := 1.0
min := 0.0
return UniformRandMinMax(min, max)
}
// RightPad2Len pads a string to a certain length for better printing
func RightPad2Len(s string, padStr string, overallLen int) string {
var padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = s + strings.Repeat(padStr, padCountInt)
return retStr[:overallLen]
}
func getExpRand(rate float64, cutoff float64, removeUnlikelyEvents bool) float64 {
var exponential = distuv.Exponential{Rate: rate}
movementTime := exponential.Rand()
if !removeUnlikelyEvents {
return movementTime
}
iterations := 0
for !(exponential.Prob(movementTime) < cutoff) {
movementTime = exponential.Rand()
iterations += 1
if iterations > unlikelyIterations {
return exponential.Mean()
}
}
return movementTime
}
func getPoissonRand(lambda float64, cutoff float64, removeUnlikelyEvents bool) float64 {
var poisson = distuv2.Poisson{Lambda: lambda}
movementTime := poisson.Rand()
if !removeUnlikelyEvents {
return movementTime
}
iterations := 0
for !(poisson.Prob(movementTime) < cutoff) {
movementTime = poisson.Rand()
iterations += 1
if iterations > unlikelyIterations {
return poisson.Mean()
}
}
return movementTime
}