Exemple #1
0
func randomData(r *rand.Rand, bytes int) []byte {
	data := make([]byte, bytes)
	for i, _ := range data {
		data[i] = byte(r.Uint32() % 256)
	}
	return data
}
Exemple #2
0
Fichier : nat.go Projet : pjump/gcc
// 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()
}
Exemple #3
0
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)
}
Exemple #4
0
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)
}
Exemple #5
0
// 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)
}
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)
}
Exemple #7
0
// 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
}
Exemple #8
0
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),
	)
}
Exemple #9
0
// 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
}
Exemple #10
0
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
}
Exemple #11
0
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()}
}
Exemple #12
0
func randBytes(r *rand.Rand, b []byte) {
	for i := range b {
		b[i] = byte(r.Uint32())
	}
}
Exemple #13
0
func (k *Int64Key) random(r *rand.Rand) {
	k.i = uint64(r.Uint32()) + uint64(r.Uint32())<<32
}
Exemple #14
0
func (k *Int32Key) random(r *rand.Rand) {
	k.i = r.Uint32()
}
Exemple #15
0
func rand8(rnd *rand.Rand) uint8 {
	return uint8(rnd.Uint32() >> 24)
}
Exemple #16
0
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)}
}
Exemple #17
0
func rand16(rnd *rand.Rand) uint16 {
	return uint16(rnd.Uint32() >> 16)
}
Exemple #18
0
func rand32(rnd *rand.Rand) uint32 {
	return rnd.Uint32()
}
Exemple #19
0
func randomInt(rand *rand.Rand) *big.Int {
	return new(big.Int).SetInt64(int64(int32(rand.Uint32())))
}
Exemple #20
0
// randUint64 makes random 64 bit numbers.
// Weirdly, rand doesn't have a function that gives you 64 random bits.
func randUint64(r *rand.Rand) uint64 {
	return uint64(r.Uint32())<<32 | uint64(r.Uint32())
}
Exemple #21
0
func randUint64(rnd *rand.Rand) uint64 {
	return uint64(rnd.Uint32())<<32 + uint64(rnd.Uint32())
}