Standard library / math/big

big.Int.Rand()

Sample a pseudorandom integer below a positive bound using math/rand. Never use for security-sensitive randomness. This method writes to the receiver (use a separate destination to preserve inputs when needed). Expected stdout: 'true\n'.

Formal Syntax & Structure

go syntaxgo
func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int

Parameters & Attributes

NameTypeDescription
rnd*rand.RandPseudorandom generator; not cryptographic.
n*IntPositive exclusive upper bound.

Return Value

*Int

Returns the result described above; see the example for the observed behavior.

Working Example

Example Snippetgo
package main
import (  "fmt"  "math/big"  "math/rand")
func main() {  n := big.NewInt(100)  z := new(big.Int).Rand(rand.New(rand.NewSource(7)), n)  fmt.Println(z.Sign() >= 0 && z.Cmp(n) < 0)}
Go Reference