Generating a random, fixed-length byte array in Go

前端 未结 3 1260
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 03:43

I have a byte array, with a fixed length of 4.

token := make([]byte, 4)

I need to set each byte to a random byte. How can I do so, in the most

3条回答
  •  鱼传尺愫
    2021-02-05 04:07

    Go 1.6 added a new function to the math/rand package:

    func Read(p []byte) (n int, err error)
    

    which fills the passed byte slice with random data. Using this rand.Read():

    token := make([]byte, 4)
    if _, err := rand.Read(token); err != nil {
        // Handle err
    }
    fmt.Println(token)
    

    rand.Read() has 2 return values: the number of "read" bytes and an (optional) error. This is to conform with the general io.Reader interface, but the documentation of rand.Read() states that (despite its signature) it will never actually return a non-nil error, so we may omit checking it, which simplifies it to this:

    token := make([]byte, 4)
    rand.Read(token)
    fmt.Println(token)
    

    Don't forget to call rand.Seed() to properly initialize it before you use the math/rand package, e.g.:

    rand.Seed(time.Now().UnixNano())
    

    Note: Prior to Go 1.6 there was no math/rand.Read() function, but there was (and still is) a crypto/rand.Read() function, but the crypto/rand package implements a cryptographically secure pseudorandom number generator, so it is much slower than math/rand.

提交回复
热议问题