Standard library / crypto/rand

crypto/rand.Int()

Generate a uniform integer in [0,max) using rejection sampling. Panics for a nonpositive maximum. Use rand.Reader for security; a modulo operation on arbitrary random bytes may bias results. Expected stdout: 'true true\n'.

Formal Syntax & Structure

go syntaxgo
func Int(rand io.Reader, max *big.Int) (n *big.Int, err error)

Parameters & Attributes

NameTypeDescription
randio.ReaderRandom source; use crypto/rand.Reader.
max*big.IntPositive exclusive upper bound.

Return Value

(n *big.Int, err error)

Uniform value and any source error.

Working Example

Example Snippetgo
package main
import (  "crypto/rand"  "fmt"  "math/big")
func main() {  n, e := rand.Int(rand.Reader, big.NewInt(10))  if e != nil {    panic(e)  }  fmt.Println(n.Sign() >= 0, n.Cmp(big.NewInt(10)) < 0)}
Go Reference