本文整理汇总了Golang中math/big.Int.Rand方法的典型用法代码示例。如果您正苦于以下问题:Golang Int.Rand方法的具体用法?Golang Int.Rand怎么用?Golang Int.Rand使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/big.Int
的用法示例。
在下文中一共展示了Int.Rand方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: benchmarkMod
func benchmarkMod(b *testing.B, w, exp uint32) {
b.StopTimer()
var n, mod big.Int
n.Rand(rand.New(rand.NewSource(1)), New(w))
n.SetBit(&n, int(w), 1)
runtime.GC()
b.StartTimer()
for i := 0; i < b.N; i++ {
Mod(&mod, &n, exp)
}
}
示例2: GenExcerciseRandomNumber
func GenExcerciseRandomNumber(lower, upper string) string {
lower = strings.TrimSpace(lower)
upper = strings.TrimSpace(upper)
var remainlowerlen int
if lowerpointpos := strings.IndexRune(lower, '.'); lowerpointpos > 0 {
remainlowerlen = len(lower[lowerpointpos+1:])
lower = lower[:lowerpointpos] + lower[lowerpointpos+1:]
}
var remainupperlen int
if upperpointpos := strings.IndexRune(upper, '.'); upperpointpos > 0 {
remainupperlen = len(upper[upperpointpos+1:])
upper = upper[:upperpointpos] + upper[upperpointpos+1:]
}
if remainlowerlen > remainupperlen {
upper += strings.Repeat("0", remainlowerlen-remainupperlen)
} else if remainlowerlen < remainupperlen {
lower += strings.Repeat("0", remainupperlen-remainlowerlen)
}
lowerbigint := new(big.Int)
lowerbigint, _ = lowerbigint.SetString(lower, 10)
upperbigint := new(big.Int)
upperbigint, _ = upperbigint.SetString(upper, 10)
diff := big.NewInt(0)
diff = diff.Sub(upperbigint, lowerbigint)
random := new(big.Int)
random = random.Rand(rand.New(rand.NewSource(time.Now().Unix())), diff)
randomnumstr := random.Add(random, lowerbigint).String()
if remainlowerlen > 0 || remainupperlen > 0 {
randompoint := len(randomnumstr) - max(remainupperlen, remainlowerlen)
randomnumstr = randomnumstr[:randompoint] + "." + randomnumstr[randompoint:]
}
if len(randomnumstr) > 1 && randomnumstr[0] == '-' && randomnumstr[1] == '.' {
randomnumstr = "-0." + randomnumstr[2:]
}
return randomnumstr
}
示例3: GenerateG
// GenerateG implements [2.2.1.2. Generation of g] in page 9
func GenerateG(p, q *big.Int) (g *big.Int) {
r := mrand.New(mrand.NewSource(time.Now().UnixNano()))
g = new(big.Int)
for {
j := new(big.Int)
j.Sub(p, big.NewInt(1))
j.Div(j, q)
h := new(big.Int)
h.Rand(r, p)
if h.Cmp(big.NewInt(0)) == 0 {
continue
}
g.Exp(h, j, p)
if g.Cmp(big.NewInt(1)) != 0 {
break
}
}
return
}
示例4: bigIntRand
// bigIntRand sets a to a random number in [origin, origin+b].
func bigIntRand(a, b *big.Int) *big.Int {
a.Rand(conf.Random(), b)
return a.Add(a, conf.BigOrigin())
}
示例5: bigIntRand
// bigIntRand sets a to a random number in [origin, origin+b].
func bigIntRand(c Context, a, b *big.Int) *big.Int {
a.Rand(c.Config().Random(), b)
return a.Add(a, c.Config().BigOrigin())
}
示例6: NewDiffieHellman
func NewDiffieHellman(g CyclicGroup) DiffieHellman {
z := new(big.Int)
z.Rand(rnd, g.Size())
return &diffieHellman{g: g, a: z}
}
示例7: PrivateKey
func PrivateKey(p *big.Int) *big.Int {
private := new(big.Int).Sub(p, two)
return private.Add(two, private.Rand(r, private))
}