Cryptographically secure pseudo-random number generator (CSPRNG)

The math and rand packages do not provide the same amount of randomness that the crypto/rand package offers. Do not use math/rand for cryptographic applications.

Read more about Go's crypto/rand package at https://golang.org/pkg/crypto/rand/.

The following example will demonstrate how to generate random bytes, a random integer, or any other signed or unsigned type of integer:

package mainimport (   "crypto/rand"   "encoding/binary"   "fmt"   "log"   "math"   "math/big")func main() {   // Generate a random int   limit := int64(math.MaxInt64) // Highest random number allowed   randInt, err := rand.Int(rand.Reader, big.NewInt(limit))   if err != nil {      log.Fatal(err)   } fmt.Println("Random int value: ...

Get Security with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.