-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrand.go
More file actions
46 lines (41 loc) · 776 Bytes
/
rand.go
File metadata and controls
46 lines (41 loc) · 776 Bytes
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 cxrand
import (
"crypto/rand"
"time"
fastrand "github.com/bytedance/gopkg/lang/fastrand"
)
func RandomBytes(b []byte) {
n := len(b)
var randNum uint32
for i := 0; i < n; i += 4 {
randNum = fastrand.Uint32()
if i+3 < n {
b[i] = byte(randNum)
b[i+1] = byte(randNum >> 8)
b[i+2] = byte(randNum >> 16)
b[i+3] = byte(randNum >> 24)
} else {
for j := 0; j < n-i; j++ {
b[i+j] = byte(randNum >> (8 * j))
}
}
}
}
func CRandomBytes(b []byte) {
_, err := rand.Read(b)
if err != nil {
panic(err)
}
}
func RandomString(b []byte) {
length := len(b)
var t int64
for i := 0; i < length; i++ {
t = time.Now().UnixNano()
randByte := byte(t & 0xff)
if randByte < 32 || randByte > 126 {
randByte = 32
}
b[i] = randByte
}
}