I have a byte array, with a fixed length of 4.
我有一个字节数组,固定长度为4。
token := make([]byte, 4)
I need to set each byte to a random byte. How can I do so, in the most efficient matter? The math/rand
methods do not provide a Random Byte function, as far as I am concerned.
我需要将每个字节设置为一个随机字节。我怎么能这样做,在最有效的事情?就我而言,math / rand方法不提供Random Byte函数。
Perhaps there is a built-in way, or should I go with generating a random string and converting it to a byte array?
也许有内置的方式,或者我应该生成随机字符串并将其转换为字节数组?
2 个解决方案
#1
14
包rand
import "math/rand"
func阅读
func Read(p []byte) (n int, err error)
Read generates len(p) random bytes from the default Source and writes them into p. It always returns len(p) and a nil error.
Read从默认Source生成len(p)个随机字节,并将它们写入p。它总是返回len(p)和nil错误。
func(* Rand)阅读
func (r *Rand) Read(p []byte) (n int, err error)
Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error.
Read生成len(p)个随机字节并将它们写入p。它总是返回len(p)和nil错误。
For example,
例如,
package main
import (
"math/rand"
"fmt"
)
func main() {
token := make([]byte, 4)
rand.Read(token)
fmt.Println(token)
}
Output:
输出:
[187 163 35 30]
#2
7
Go 1.6 added a new function to the math/rand
package:
Go 1.6在math / rand包中添加了一个新函数:
func Read(p []byte) (n int, err error)
which fills the passed byte
slice with random data. Using this rand.Read()
:
用随机数据填充传递的字节片。使用这个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:
rand.Read()有2个返回值:“读取”字节数和(可选)错误。这是为了符合通用的io.Reader接口,但rand.Read()的文档声明(尽管它的签名)它永远不会实际返回非零错误,所以我们可以省略检查它,这简化了它这个:
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.:
在使用math / rand包之前,不要忘记调用rand.Seed()来正确初始化它,例如:
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
.
注意:在Go 1.6之前没有math / rand.Read()函数,但是有(现在仍然是)crypto / rand.Read()函数,但是crypto / rand包实现了一个加密安全的伪随机数生成器,所以它比math / rand慢得多。
#1
14
包rand
import "math/rand"
func阅读
func Read(p []byte) (n int, err error)
Read generates len(p) random bytes from the default Source and writes them into p. It always returns len(p) and a nil error.
Read从默认Source生成len(p)个随机字节,并将它们写入p。它总是返回len(p)和nil错误。
func(* Rand)阅读
func (r *Rand) Read(p []byte) (n int, err error)
Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error.
Read生成len(p)个随机字节并将它们写入p。它总是返回len(p)和nil错误。
For example,
例如,
package main
import (
"math/rand"
"fmt"
)
func main() {
token := make([]byte, 4)
rand.Read(token)
fmt.Println(token)
}
Output:
输出:
[187 163 35 30]
#2
7
Go 1.6 added a new function to the math/rand
package:
Go 1.6在math / rand包中添加了一个新函数:
func Read(p []byte) (n int, err error)
which fills the passed byte
slice with random data. Using this rand.Read()
:
用随机数据填充传递的字节片。使用这个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:
rand.Read()有2个返回值:“读取”字节数和(可选)错误。这是为了符合通用的io.Reader接口,但rand.Read()的文档声明(尽管它的签名)它永远不会实际返回非零错误,所以我们可以省略检查它,这简化了它这个:
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.:
在使用math / rand包之前,不要忘记调用rand.Seed()来正确初始化它,例如:
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
.
注意:在Go 1.6之前没有math / rand.Read()函数,但是有(现在仍然是)crypto / rand.Read()函数,但是crypto / rand包实现了一个加密安全的伪随机数生成器,所以它比math / rand慢得多。