crypto/rand

Crypto
import "crypto/rand"

Implements a cryptographically secure random number generator.

Example

package main

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

func main() {
    b := make([]byte, 16)
    rand.Read(b)
    fmt.Println(hex.EncodeToString(b))
}

Key Types & Functions

ReadReaderIntPrime

About crypto/rand

The crypto/rand package (imported as crypto/rand) belongs to the Crypto category of Go packages. Implements a cryptographically secure random number generator.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The crypto/rand package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using crypto/rand in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages