github.com/orangebees/go-oneutils@v0.0.10/Random/func.go (about) 1 package Random 2 3 const hextable = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 4 const hextable64 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01" 5 6 // RandBytes 快速生成n位随机字节 7 func RandBytes(n int) []byte { 8 b := make([]byte, n) 9 for i := 0; i < n; i++ { 10 b[i] = hextable64[RandInt64()] 11 } 12 return b 13 } 14 15 // RandBytes32 快速生成32位随机字节 16 func RandBytes32() []byte { 17 b := make([]byte, 32) 18 for i := 0; i < 32; i++ { 19 b[i] = hextable64[RandInt64()] 20 } 21 return b 22 } 23 24 // RandIntn 快速生成num范围内的随机数 25 func RandIntn(num uint32) uint32 { 26 return FastRand() % num 27 } 28 29 // RandInt64 快速生成64内的随机数 0-63 30 func RandInt64() uint32 { 31 i := FastRand() & 63 32 return i 33 } 34 func RandInt36() uint32 { 35 return FastRand() & 35 36 } 37 38 // UUIDv4 快速生成UUIDv4 39 func UUIDv4() []byte { 40 b := make([]byte, 36) 41 for i := 0; i < 30; i++ { 42 b[i] = hextable[FastRand()&35] 43 } 44 b[30], b[31], b[32], b[33], b[34], b[35], 45 b[8], b[13], b[14], b[18], b[19], b[23] = b[8], b[13], b[14], b[18], b[19], b[23], 46 '-', '-', '4', '-', 'a', '-' 47 return b 48 }