forked from grafana/k6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoding.go
38 lines (31 loc) · 849 Bytes
/
encoding.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
package tracing
import (
"math/rand"
)
// randHexStringRunes returns a random string of n hex characters.
//
// Note that this function uses a non-cryptographic random number generator.
func randHexString(n int) string {
hexRunes := []rune("123456789abcdef")
b := make([]rune, n)
for i := range b {
b[i] = hexRunes[rand.Intn(len(hexRunes))] //nolint:gosec
}
return string(b)
}
// chance returns true with a `percentage` chance, otherwise false.
// the `percentage` argument is expected to be
// within 0 <= percentage <= 100 range.
//
// The chance function works under the assumption that the
// go rand module has been seeded with a non-deterministic
// value.
func chance(r *rand.Rand, percentage float64) bool {
if percentage == 0.0 {
return false
}
if percentage == 1.0 {
return true
}
return r.Float64() < percentage
}