1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:14:47 -07:00

big: add random number generation

Adds func (z *Int) RandIntn(src rand.Source,n *Int) *Int

R=rsc
CC=golang-dev, gri
https://golang.org/cl/2315045
This commit is contained in:
Florian Uekermann 2010-10-18 14:09:20 -04:00 committed by Russ Cox
parent ee3db0f5cf
commit e9c35ac55d

View File

@ -6,8 +6,10 @@
package big
import "fmt"
import (
"fmt"
"rand"
)
// An Int represents a signed multi-precision integer.
// The zero value for an Int represents the value 0.
@ -545,6 +547,18 @@ func ProbablyPrime(z *Int, n int) bool {
}
// Rand sets z to a pseudo-random number in [0, n) and returns z.
func (z *Int) Rand(rnd *rand.Rand, n *Int) *Int {
z.neg = false
if n.neg == true || len(n.abs) == 0 {
z.abs = nil
return z
}
z.abs = z.abs.random(rnd, n.abs, n.abs.bitLen())
return z
}
// ModInverse sets z to the multiplicative inverse of g in the group /p (where
// p is a prime) and returns z.
func (z *Int) ModInverse(g, p *Int) *Int {