-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
33 lines (28 loc) · 879 Bytes
/
random.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
package tplfuncs
import (
htmlTemplate "html/template"
"math/rand"
textTemplate "text/template"
"time"
)
// RandomHelpers returns a text template FuncMap with random related functions
func RandomHelpers() textTemplate.FuncMap {
return textTemplate.FuncMap{
"seededRandom": seededRandomFunc,
"random": randomFunc,
}
}
// RandomHelpersHTML returns an HTML template FuncMap with random related functions
func RandomHelpersHTML() htmlTemplate.FuncMap {
return htmlTemplate.FuncMap(RandomHelpers())
}
// Doc: `seededRandom` returns a rand.Source that is seeded with the given int value.
func seededRandomFunc(seed int) *rand.Rand {
s := rand.NewSource(int64(seed))
return rand.New(s)
}
// Doc: `random` returns a rand.Source that is seeded with the current time.
func randomFunc() *rand.Rand {
s := rand.NewSource(time.Now().UnixNano())
return rand.New(s)
}