当前位置: 首页>>代码示例>>Golang>>正文


Golang Int.Rand方法代码示例

本文整理汇总了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)
	}
}
开发者ID:klizhentas,项目名称:acbuild,代码行数:11,代码来源:all_test.go

示例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
}
开发者ID:sguzwf,项目名称:algorithm,代码行数:46,代码来源:webzapfen.go

示例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
}
开发者ID:obinnus,项目名称:diffie-hellman,代码行数:20,代码来源:dh.go

示例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())
}
开发者ID:nathangrigg,项目名称:ivy,代码行数:5,代码来源:unary.go

示例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())
}
开发者ID:db47h,项目名称:ivy,代码行数:5,代码来源:unary.go

示例6: NewDiffieHellman

func NewDiffieHellman(g CyclicGroup) DiffieHellman {
	z := new(big.Int)
	z.Rand(rnd, g.Size())
	return &diffieHellman{g: g, a: z}
}
开发者ID:hundt,项目名称:crypto-challenges,代码行数:5,代码来源:diffie.go

示例7: PrivateKey

func PrivateKey(p *big.Int) *big.Int {
	private := new(big.Int).Sub(p, two)
	return private.Add(two, private.Rand(r, private))
}
开发者ID:ncsibra,项目名称:xgo,代码行数:4,代码来源:example.go


注:本文中的math/big.Int.Rand方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。