aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Uekermann <florian@uekermann-online.de>2010-10-18 14:09:20 -0400
committerRuss Cox <rsc@golang.org>2010-10-18 14:09:20 -0400
commite9c35ac55d68b2fa57aea392d29687a67098c934 (patch)
tree95d31225528bc222e6b4ada1663c334f1b30b3e5
parentee3db0f5cf3d71f82064ea6b87690a0c3bc4b783 (diff)
downloadgo-e9c35ac55d68b2fa57aea392d29687a67098c934.tar.gz
go-e9c35ac55d68b2fa57aea392d29687a67098c934.zip
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
-rwxr-xr-xsrc/pkg/big/int.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/pkg/big/int.go b/src/pkg/big/int.go
index c78532011e..46e0087343 100755
--- a/src/pkg/big/int.go
+++ b/src/pkg/big/int.go
@@ -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 {