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

时间:2017-10-24 07:26:25

标签: string performance go random hex

我需要生成很多固定长度的随机十六进制字符串。 我找到了这个解决方案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: 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

=&GT; icza RandStringBytesMaskImprSrc解决方案效率更高

3 个答案:

答案 0 :(得分:3)

实际上,您发布的代码会运行,即使其中存在错误(见下文),但它仍然不会引起恐慌(只会让性能变差)。

您发布的堆栈跟踪表示math/rand包中的错误,我没有遇到过。请发布完整代码和Go version + env(go versiongo env)。

恐慌/解决方案的原因:

事实证明,提问者同时从多个goroutines调用RandStringBytesMaskImprSrc()RandStringBytesMaskImprSrc()使用共享的rand.Source实例,这对于并发使用是不安全的,因此来自math/rand包的恐慌。修复是为每个goroutines创建单独的rand.Source(),并将其传递给RandStringBytesMaskImprSrc()

&#34;配置中出现错误&#34;开头的常数:

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
)

常量letterIdxBits应包含表示符号索引所需的位数。由于您使用的是16个元素的字母表(letterBytes的长度),因此16种组合仅需要4位:

letterIdxBits = 4                    // 4 bits to represent a letter index

测试示例:

var tryArr = make([]string, 10)
for i := range tryArr {
    tryArr[i] = RandStringBytesMaskImprSrc(8)
}
fmt.Println(tryArr)

输出(在Go Playground上尝试):

[d3e7caa6 a69c9b7d c37a613b 92d5a43b 64059c4a 4f08141b 70130c65 1546daaf fe140fcd 0d714e4d]

(注意:由于Go操场上的开始时间是固定的并且输出被缓存,因此您将始终看到这些随机生成的字符串。在您的机器上运行它以查看随机结果。)

答案 1 :(得分:2)

首先,使用适当的生成器替换您的rand源:

var rnd = rand.New(src)

然后只需使用标准解决方案格式化数字:

fmt.Sprintf("%x", rnd.Uint64())

替代地;

strconv.FormatUint(rnd.Uint64(), 16)

这两种方法都比你的更快(在它被修复之后):

BenchmarkRandStringBytesMaskImprSrc-4       10000000           196 ns/op
BenchmarkFmt-4                              10000000           148 ns/op
BenchmarkStrconv-4                          20000000            89.8 ns/op

答案 2 :(得分:2)

* math / rand.Rand是一个io.Reader,所以读取N个随机字节然后对它们进行hexencode是很简单的:

package main

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

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]
}