本文整理汇总了Golang中math/rand.Rand.Uint32方法的典型用法代码示例。如果您正苦于以下问题:Golang Rand.Uint32方法的具体用法?Golang Rand.Uint32怎么用?Golang Rand.Uint32使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/rand.Rand
的用法示例。
在下文中一共展示了Rand.Uint32方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: randomData
func randomData(r *rand.Rand, bytes int) []byte {
data := make([]byte, bytes)
for i, _ := range data {
data[i] = byte(r.Uint32() % 256)
}
return data
}
示例2: random
// random creates a random integer in [0..limit), using the space in z if
// possible. n is the bit length of limit.
func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
if alias(z, limit) {
z = nil // z is an alias for limit - cannot reuse
}
z = z.make(len(limit))
bitLengthOfMSW := uint(n % _W)
if bitLengthOfMSW == 0 {
bitLengthOfMSW = _W
}
mask := Word((1 << bitLengthOfMSW) - 1)
for {
switch _W {
case 32:
for i := range z {
z[i] = Word(rand.Uint32())
}
case 64:
for i := range z {
z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32
}
default:
panic("unknown word size")
}
z[len(limit)-1] &= mask
if z.cmp(limit) < 0 {
break
}
}
return z.norm()
}
示例3: MakeRandom
func MakeRandom(gen *rand.Rand, degree int) *Polynomial {
if degree == 0 {
return NewPolynomialFromInt(gen.Int63n(2))
}
coeffs := new(big.Int)
// x^0 + x^1 + ... + x^n => n + 1 terms
// However, only the first n terms are variable. (x^n is fixed to
// have degree n.) Thus, we randomly generate the first n terms
// and fix the final term x^n.
numBits := degree
numBlocks := numBits / 32
for ii := 0; ii < numBlocks; ii++ {
v := gen.Uint32()
// Merge.
bigV := big.NewInt(int64(v))
coeffs.Lsh(coeffs, 32).Or(coeffs, bigV)
}
// Handle the remainder.
numRemainingBits := uint(numBits % 32)
if numRemainingBits > 0 {
mask := (int64(1) << numRemainingBits) - 1
v := int64(gen.Uint32()) & mask
coeffs.Lsh(coeffs, numRemainingBits).Or(coeffs, big.NewInt(v))
}
coeffs.SetBit(coeffs, degree, 1)
return NewPolynomial(uint(degree), coeffs)
}
示例4: Xer
func (_ Test) Xer() {
var rng *rand.Rand
rng = rand.New(xer.New(time.Now().UnixNano(), 256))
resultI := make([]uint8, 70)
for i, _ := range resultI {
resultI[i] = uint8(rng.Uint32()) >> 4
}
e.InfoLog.Println("Xer Random: ", resultI)
}
示例5: Generate
// Generate implements testing/quick.Generator.
func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value {
m := rand.Intn(len(h))
for i := len(h) - 1; i > m; i-- {
h[i] = byte(rand.Uint32())
}
return reflect.ValueOf(h)
}
示例6: Generate
func (NodeID) Generate(rand *rand.Rand, size int) reflect.Value {
var id NodeID
m := rand.Intn(len(id))
for i := len(id) - 1; i > m; i-- {
id[i] = byte(rand.Uint32())
}
return reflect.ValueOf(id)
}
示例7: New32
// Construct new Bloom filter with given number of buckets and hashes.
//
// The random seed r is used to construct the hash functions.
// The number of buckets will be rounded up to a multiple of 32.
//
// Returns nil and an error if the number of buckets exceeds (1<<32)-1.
func New32(nbuckets, nhashes int, r *rand.Rand) (f *Filter32, err error) {
// Round to next multiple of 32.
nmod32 := nbuckets & ((1 << 5) - 1)
if nmod32 != 0 {
nbuckets += 32 - nmod32
}
if nbuckets > math.MaxUint32 {
err = fmt.Errorf("nbuckets = %d (after rounding) too large", nbuckets)
return
}
seeds := make([]uint32, nhashes)
for i := range seeds {
seeds[i] = r.Uint32()
}
f = &Filter32{make([]uint32, nbuckets>>5), seeds}
return
}
示例8: TestBloomFilter
func TestBloomFilter(r *rand.Rand, size, numAdd, numSample, numHash uint) {
falsePositive := 0
falseNegative := 0 // should not occur!
truePositive := 0
trueNegative := 0
iterations := 10000
for iteration := 0; iteration < iterations; iteration++ {
hashes := make([]uint64, numHash, numHash)
for i := uint(0); i < numHash; i++ {
hashes[i] = uint64(r.Uint32())
}
bloom := New(size, hashes)
realset := make(map[uint64]bool)
generate := func() uint64 {
return uint64(r.Int63())
}
for i := uint(0); i < numAdd; i++ {
generated := generate()
bloom.Add(generated)
realset[generated] = true
}
for i := uint(0); i < numSample; i++ {
generated := generate()
bloomResult := bloom.MaybeContains(generated)
_, realResult := realset[generated]
if bloomResult && realResult {
truePositive++
} else if !bloomResult && !realResult {
trueNegative++
} else if bloomResult && !realResult {
falsePositive++
} else if !bloomResult && realResult {
falseNegative++
}
}
}
fmt.Printf(" %12d %12d %12d | %02.4f\n", size, numAdd, numHash,
float32(falsePositive)/float32(numSample)*100/float32(iterations),
)
}
示例9: Rand
// 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.doinit()
// Get rid of n <= 0 case
if n.Sign() <= 0 {
z.SetInt64(0)
return z
}
// Make a copy of n if aliased
t := n
aliased := false
if n == z {
aliased = true
t = new(Int).Set(n)
}
// Work out bit sizes and masks
bits := n.BitLen() // >= 1
nwords := (bits + 31) / 32 // >= 1
bitLengthOfMSW := uint(bits % 32)
if bitLengthOfMSW == 0 {
bitLengthOfMSW = 32
}
mask := uint32((1 << bitLengthOfMSW) - 1)
words := make([]uint32, nwords)
for {
// Make a most significant first array of random bytes
for i := 0; i < nwords; i++ {
words[i] = rnd.Uint32()
}
// Mask out the top bits so this is only just bigger than n
words[0] &= mask
C.mpz_import(&z.i[0], C.size_t(len(words)), 1, 4, 0, 0, unsafe.Pointer(&words[0]))
// Exit if z < n - should take ~1.5 iterations of loop on average
if z.Cmp(t) < 0 {
break
}
}
if aliased {
t.Clear()
}
return z
}
示例10: randBytes
func randBytes(
r *rand.Rand,
n int) (b []byte) {
b = make([]byte, n)
fullWords := n / 4
remainder := n % 4
for i := 0; i < fullWords; i++ {
u32 := r.Uint32()
b[4*i+0] = byte((u32 >> 0) & 0xff)
b[4*i+1] = byte((u32 >> 8) & 0xff)
b[4*i+2] = byte((u32 >> 16) & 0xff)
b[4*i+3] = byte((u32 >> 24) & 0xff)
}
for i := 0; i < remainder; i++ {
b[4*fullWords+i] = byte(r.Uint32())
}
return
}
示例11: NewFromRandom
func NewFromRandom(r *rand.Rand) *Number160 {
// there is no rand.Uint64(). Seems that it was forgotten:
// https://groups.google.com/forum/#!topic/golang-nuts/Kle874lT1Eo
u1 := uint64(r.Uint32())<<32 + uint64(r.Uint32())
u2 := uint64(r.Uint32())<<32 + uint64(r.Uint32())
return &Number160{u1, u2, r.Uint32()}
}
示例12: randBytes
func randBytes(r *rand.Rand, b []byte) {
for i := range b {
b[i] = byte(r.Uint32())
}
}
示例13: random
func (k *Int64Key) random(r *rand.Rand) {
k.i = uint64(r.Uint32()) + uint64(r.Uint32())<<32
}
示例14: rand8
func rand8(rnd *rand.Rand) uint8 {
return uint8(rnd.Uint32() >> 24)
}
示例15: randomIPv4Address
func randomIPv4Address(b *testing.B, r *rand.Rand) net.IP {
num := r.Uint32()
return []byte{byte(num >> 24), byte(num >> 16), byte(num >> 8),
byte(num)}
}