Golang crypto/rand.Int() function example

package crypto/rand

Int returns a uniform random value in [0, max). *max is the given input (2nd parameter). The function will panic if max <= 0.

Golang crypto/rand.Int() function usage example

 package main

 import (
 "crypto/rand"
 "fmt"
 "math/big"
 )

 func main() {
 var n *big.Int
 var err error

 max := *big.NewInt(99999999999)

 n, err = rand.Int(rand.Reader, &max)

 if err != nil {
 fmt.Println(err)
 }

 fmt.Printf("%d\n", n)

 }

Example output :

go run cryptorand.go

48752046463

Reference :

http://golang.org/pkg/crypto/rand/#Int

Advertisement