gpt4 book ai didi

string - 在 Golang 中生成固定长度的随机十六进制字符串的有效方法?

转载 作者:IT王子 更新时间:2023-10-29 01:25:48 26 4
gpt4 key购买 nike

我需要生成大量固定长度的随机十六进制字符串。我找到这个解决方案 How to generate a random string of a fixed length in golang?

我正在做这样的事情:

const letterBytes = "abcdef0123456789"
const (
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

var src = rand.NewSource(time.Now().UnixNano())

// RandStringBytesMaskImprSrc ...
// Src: https://stackoverflow.com/a/31832326/710955
func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}

return string(b)
}

var tryArr = make([]string, 10000)
for i := 0; i < 10000; i++ {
tryArr[i] = RandStringBytesMaskImprSrc(8)
}

但是我遇到了这个 panic 错误

panic: runtime error: index out of range

goroutine 36 [running]:
math/rand.(*rngSource).Int63(0x11bb1300, 0x8, 0x8)
D:/Applications/Go/src/math/rand/rng.go:231 +0xa0
main.RandStringBytesMaskImprSrc(0x8, 0x11f81be8, 0x8)
main.go:60 +0x5f

错误似乎在 for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0;,但我不明白为什么会出现这个错误。

在 Go 中生成大量固定长度的随机十六进制字符串的最快和最简单的方法是什么?

基准

package bench

import (
"encoding/hex"
"math/rand"
"testing"
"time"
)

const letterBytes = "abcdef0123456789"
const (
letterIdxBits = 4 // 4 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)

var src1 = rand.NewSource(time.Now().UnixNano())
var src2 = rand.New(rand.NewSource(time.Now().UnixNano()))

// RandStringBytesMaskImprSrc returns a random hexadecimal string of length n.
func RandStringBytesMaskImprSrc1(n int) string {
b := make([]byte, n)
for i, cache, remain := n-1, src1.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src1.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}

return string(b)
}

func RandStringBytesMaskImprSrc2(n int) string {
b := make([]byte, (n+1)/2) // can be simplified to n/2 if n is always even

if _, err := src2.Read(b); err != nil {
panic(err)
}

return hex.EncodeToString(b)[:n]
}

func BenchmarkRandStringBytesMaskImprSrc1(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = RandStringBytesMaskImprSrc1(8)
}
}

func BenchmarkRandStringBytesMaskImprSrc2(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = RandStringBytesMaskImprSrc2(8)
}
}


goos: windows
goarch: 386
BenchmarkRandStringBytesMaskImprSrc1-4 20000000 116 ns/op 16 B/op 2 allocs/op
BenchmarkRandStringBytesMaskImprSrc2-4 10000000 231 ns/op 24 B/op 3 allocs/op
PASS
ok command-line-arguments 5.139s

=> icza RandStringBytesMaskImprSrc解决方案效率更高

最佳答案

*math/rand.Rand 是一个 io.Reader,因此读取 N 个随机字节然后对它们进行十六进制编码是微不足道的:

package main

import (
"encoding/hex"
"fmt"
"math/rand"
"time"
)

var src = rand.New(rand.NewSource(time.Now().UnixNano()))

func main() {
fmt.Println(RandStringBytesMaskImprSrc(4))
}

// RandStringBytesMaskImprSrc returns a random hexadecimal string of length n.
func RandStringBytesMaskImprSrc(n int) string {
b := make([]byte, (n+1)/2) // can be simplified to n/2 if n is always even

if _, err := src.Read(b); err != nil {
panic(err)
}

return hex.EncodeToString(b)[:n]
}

关于string - 在 Golang 中生成固定长度的随机十六进制字符串的有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46904588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com