Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to generate a long random string in Go?

Tags:

string

random

go

Like [a-zA-Z0-9] string:

na1dopW129T0anN28udaZ

or hexadecimal string:

8c6f78ac23b4a7b8c0182d

By long I mean 2K and more characters.

like image 397
Pavel Paulau Avatar asked Oct 07 '12 19:10

Pavel Paulau


People also ask

How do you generate a random string of a fixed length in go?

Previous solutions get a random number to designate a random letter by calling rand. Intn() which delegates to Rand. Intn() which delegates to Rand. Int31n() .

How do you generate random characters in Golang?

Next up, we need to randomly choose a character from the string, we can do that by importing the rand package in Golang. The rand function has a function called Intn, which generates a random number between 0 (inclusive) to the provided number(non-inclusive).

How do you generate unique random strings?

There are many ways to generate a random, unique, alphanumeric string in PHP which are given below: Using str_shuffle() Function: The str_shuffle() function is an inbuilt function in PHP and is used to randomly shuffle all the characters of a string passed to the function as a parameter.

How do you generate random values in Golang?

Golang has built-in support for random number generation in the standard library. Specifically there is the math/rand package which implements pseudo-random number generators. Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source.


1 Answers

This does about 200MBps on my box. There's obvious room for improvement.

type randomDataMaker struct {
    src rand.Source
}

func (r *randomDataMaker) Read(p []byte) (n int, err error) {
    for i := range p {
        p[i] = byte(r.src.Int63() & 0xff)
    }
    return len(p), nil
}

You'd just use io.CopyN to produce the string you want. Obviously you could adjust the character set on the way in or whatever.

The nice thing about this model is that it's just an io.Reader so you can use it making anything.

Test is below:

func BenchmarkRandomDataMaker(b *testing.B) {
    randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)}
    for i := 0; i < b.N; i++ {
        b.SetBytes(int64(i))
        _, err := io.CopyN(ioutil.Discard, &randomSrc, int64(i))
        if err != nil {
            b.Fatalf("Error copying at %v: %v", i, err)
        }
    }
}

On one core of my 2.2GHz i7:

BenchmarkRandomDataMaker       50000        246512 ns/op     202.83 MB/s

EDIT

Since I wrote the benchmark, I figured I'd do the obvious improvement thing (call out to the random less frequently). With 1/8 the calls to rand, it runs about 4x faster, though it's a big uglier:

New version:

func (r *randomDataMaker) Read(p []byte) (n int, err error) {
    todo := len(p)
    offset := 0
    for {
        val := int64(r.src.Int63())
        for i := 0; i < 8; i++ {
            p[offset] = byte(val & 0xff)
            todo--
            if todo == 0 {
                return len(p), nil
            }
            offset++
            val >>= 8
        }
    }

    panic("unreachable")
}

New benchmark:

BenchmarkRandomDataMaker      200000        251148 ns/op     796.34 MB/s

EDIT 2

Took out the masking in the cast to byte since it was redundant. Got a good deal faster:

BenchmarkRandomDataMaker      200000        231843 ns/op     862.64 MB/s

(this is so much easier than real work sigh)

EDIT 3

This came up in irc today, so I released a library. Also, my actual benchmark tool, while useful for relative speed, isn't sufficiently accurate in its reporting.

I created randbo that you can reuse to produce random streams wherever you may need them.

like image 155
Dustin Avatar answered Sep 19 '22 21:09

Dustin