當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。