Example #1
0
func rndBytes(rng *rand.Rand, n int) []byte {
	r := make([]byte, n)
	for i := range r {
		r[i] = byte(rng.Int())
	}
	return r
}
Example #2
0
func main() {

	var (
		nbLigne uint
		result  [][]int
		r       *rand.Rand
		i       uint
	)

	nbLigne = Initialisation()

	result = make([][]int, nbLigne)

	r = rand.New(rand.NewSource(time.Now().UnixNano()))

	for i = 0; i < nbLigne; i++ {
		result[i] = make([]int, 3)
		result[i][0] = int(math.Mod(float64(r.Int()), 200)) - 100
		result[i][1] = int(math.Mod(float64(r.Int()), 200)) - 100
		result[i][2] = int(math.Mod(float64(r.Int()), 9)) + 1

	}

	ecrireFichier("fichierTest", nbLigne, result)
}
Example #3
0
func (g *game) Tick(p *rand.Rand) (food bool, selfeat []int) {
	//	r := rand.New(rand.NewSource(p))
	var tempTail points
	for i, sn := range g.Snake {
		tempTail = g.Snake[i].Body[len(sn.Body)-1]
		for q, _ := range sn.Body[:len(sn.Body)-1] {
			g.Snake[i].Body[len(sn.Body)-q-1] = g.Snake[i].Body[len(sn.Body)-q-2]
		}
		switch sn.Dir {
		case "UP":
			g.Snake[i].Body[0].Y--
		case "DOWN":
			g.Snake[i].Body[0].Y++
		case "LEFT":
			g.Snake[i].Body[0].X--
		case "RIGHT":
			g.Snake[i].Body[0].X++
		}
		food = g.EatFood(tempTail)
		selfeat = g.EatSelf()
		if len(g.Food) == 0 {
			g.AddFood(p.Int()%30, p.Int()%30)
		}
		fmt.Println(sn)
	}
	return food, selfeat
}
Example #4
0
// Generate array of random data for transmission
func genData(n int, rgen rand.Rand) []int {
	result := make([]int, n)
	for i := 0; i < n; i++ {
		result[i] = rgen.Int()
	}
	return result
}
Example #5
0
func Qsort(a Interface, prng *rand.Rand) Interface {
	if a.Len() < 2 {
		return a
	}

	left, right := 0, a.Len()-1

	pivotIndex := prng.Int() % a.Len()
	a.Swap(pivotIndex, right)

	for i := 0; i < a.Len(); i++ {
		if a.Less(i, right) {

			a.Swap(i, left)
			left++
		}
	}

	a.Swap(left, right)

	leftSide, rightSide := a.Partition(left)
	Qsort(leftSide, prng)
	Qsort(rightSide, prng)

	return a
}
func TestPriorityQueue_order_Time(t *testing.T) {
	pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
		return x.(time.Time).Before(y.(time.Time))
	}), 10)

	//Populate the priority queue with random times
	var src rand.Source = rand.NewSource(0)
	var r *rand.Rand = rand.New(src)
	for i := 0; i < 10; i++ {
		assert.True(
			t,
			pq.Length() == i,
			"pq.Length() = %d; want %d", pq.Length(), i,
		)
		pq.Insert(time.Now().Add(time.Hour * time.Duration(r.Int())))
	}
	var prev time.Time = pq.PopTop().(time.Time)
	var next time.Time
	for pq.Length() > 0 {
		next = pq.PopTop().(time.Time)
		assert.True(
			t,
			prev.Before(next),
			"%s sorted before %s; want %s sorted after %s", prev, next, prev, next,
		)
	}
}
Example #7
0
func (self *Scene) Emitter(r *rand.Rand) *memit.Emitter {
	if len(self.emitters) == 0 {
		return nil
	}
	i := r.Int() % len(self.emitters)
	return &self.emitters[i]
}
Example #8
0
//Shuffle cards in the deck
func (deck *Deck) Shuffle(r *rand.Rand) {
	data := *deck
	l := len(data)
	for i := 0; i < l; i++ {
		j := r.Int() % (i + 1)
		data[i], data[j] = data[j], data[i]
	}
}
Example #9
0
func randomString(prng *rand.Rand, length int) string {
	var b bytes.Buffer

	for i := 0; i < length; i += 1 {
		b.WriteByte(byte(prng.Int() % 256))
	}
	return b.String()
}
Example #10
0
func (r randomSplitter) Generate(rand *rand.Rand, size int) reflect.Value {
	s := rand.Int() % 8
	b := make([]byte, s)
	for i, _ := range b {
		b[i] = split[rand.Int()%3]
	}
	return reflect.ValueOf(randomSplitter(b))
}
Example #11
0
// Returns a point chosen such that each pixel on the image has an
// equally likely chance of being painted; we do this by sampling
// points up to one radius away from the edges of the image.
func unifPoint(img image.Image, r int, rnd *rand.Rand) (int, int) {
	bounds := img.Bounds()

	dx := (bounds.Max.X - bounds.Min.X)
	dy := (bounds.Max.Y - bounds.Min.Y)
	x := bounds.Min.X + rnd.Int()%dx
	y := bounds.Min.Y + rnd.Int()%dy

	return x, y
}
Example #12
0
// randomPset returns a parallel set of random size and elements.
func randomPset(prng *rand.Rand, maxSize int) *pset {
	set := makePset()
	size := int(prng.Int()) % maxSize
	for i := 0; i < size; i++ {
		// TODO(adonovan): benchmark how performance varies
		// with this sparsity parameter.
		n := int(prng.Int()) % 10000
		set.add(n)
	}
	return set
}
func TestPriorityQueue_order_int(t *testing.T) {
	pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
		return x.(int) < y.(int)
	}), 0)

	//Populate the priority queue with random ints
	var src rand.Source = rand.NewSource(0)
	var r *rand.Rand = rand.New(src)
	items := make(map[int]bool)
	for i := 0; i < 10; i++ {
		assert.True(
			t,
			pq.Length() == i,
			"pq.Length() = %d; want %d", pq.Length(), i,
		)
		n := r.Int()
		pq.Insert(n)
		items[n] = true
	}
	// test Items
	sortedItems := make([]int, len(items))
	ctr := 0
	for n, _ := range items {
		sortedItems[ctr] = n
		ctr++
	}
	sort.Ints(sortedItems)
	recItems := pq.Items()
	recInts := make([]int, len(recItems))
	for i, n := range recItems {
		recInts[i] = n.(int)
	}
	sort.Ints(recInts)
	for i, n := range recInts {
		assert.True(
			t,
			sortedItems[i] == n,
			"put and recovered items %d, &d not equal", sortedItems[i], n,
		)
	}
	// test order
	var prev int = pq.PopTop().(int)
	var next int
	for pq.Length() > 0 {
		next = pq.PopTop().(int)
		assert.True(
			t,
			prev <= next,
			"%d sorted before %d; want %d sorted after %d", prev, next, prev, next,
		)
	}
}
Example #14
0
func (r *Consumer) redistributeRDY(rng *rand.Rand) {
	if r.inBackoffBlock() {
		return
	}

	numConns := int32(len(r.conns()))
	maxInFlight := r.getMaxInFlight()
	if numConns > maxInFlight {
		r.log(LogLevelDebug, "redistributing RDY state (%d conns > %d max_in_flight)",
			numConns, maxInFlight)
		atomic.StoreInt32(&r.needRDYRedistributed, 1)
	}

	if r.inBackoff() && numConns > 1 {
		r.log(LogLevelDebug, "redistributing RDY state (in backoff and %d conns > 1)", numConns)
		atomic.StoreInt32(&r.needRDYRedistributed, 1)
	}

	if !atomic.CompareAndSwapInt32(&r.needRDYRedistributed, 1, 0) {
		return
	}

	conns := r.conns()
	possibleConns := make([]*Conn, 0, len(conns))
	for _, c := range conns {
		lastMsgDuration := time.Now().Sub(c.LastMessageTime())
		rdyCount := c.RDY()
		r.log(LogLevelDebug, "(%s) rdy: %d (last message received %s)",
			c.String(), rdyCount, lastMsgDuration)
		if rdyCount > 0 && lastMsgDuration > r.config.LowRdyIdleTimeout {
			r.log(LogLevelDebug, "(%s) idle connection, giving up RDY", c.String())
			r.updateRDY(c, 0)
		}
		possibleConns = append(possibleConns, c)
	}

	availableMaxInFlight := int64(maxInFlight) - atomic.LoadInt64(&r.totalRdyCount)
	if r.inBackoff() {
		availableMaxInFlight = 1 - atomic.LoadInt64(&r.totalRdyCount)
	}

	for len(possibleConns) > 0 && availableMaxInFlight > 0 {
		availableMaxInFlight--
		i := rng.Int() % len(possibleConns)
		c := possibleConns[i]
		// delete
		possibleConns = append(possibleConns[:i], possibleConns[i+1:]...)
		r.log(LogLevelDebug, "(%s) redistributing RDY", c.String())
		r.updateRDY(c, 1)
	}
}
Example #15
0
func (_ VbmapParams) Generate(rand *rand.Rand, size int) reflect.Value {
	nodes := rand.Int()%100 + 1
	replicas := rand.Int() % 4

	params = VbmapParams{
		Tags:        trivialTags(nodes),
		NumNodes:    nodes,
		NumSlaves:   10,
		NumVBuckets: 1024,
		NumReplicas: replicas,
	}
	normalizeParams(&params)

	return reflect.ValueOf(params)
}
Example #16
0
func (Opaque) Generate(rand *rand.Rand, size int) reflect.Value {
	var u Opaque
	for i := range u[:] {
		u[i] = byte(rand.Int())
	}
	return reflect.ValueOf(u)
}
Example #17
0
File: quick.go Project: achanda/go
// randFloat64 generates a random float taking the full range of a float64.
func randFloat64(rand *rand.Rand) float64 {
	f := rand.Float64() * math.MaxFloat64
	if rand.Int()&1 == 1 {
		f = -f
	}
	return f
}
Example #18
0
func ExprGenDepth(egp *TreeParams, rng *rand.Rand) Expr {
	rnum := rng.Int()
	var e ExprType

	if egp.CurrDepth >= egp.TmpMaxDepth {
		e = egp.LeafsT[rnum%len(egp.LeafsT)]
	} else {
		if egp.InTrig {
			e = egp.NonTrigT[rnum%len(egp.NonTrigT)]
		} else {
			e = egp.NodesT[rnum%len(egp.NodesT)] // this is to deal with NULL (ie so we dont get switch on 0)
		}
	}

	return exprGrow(e, ExprGenDepth, egp, rng)
}
Example #19
0
func (_ EqualTagsR1VbmapParams) Generate(rand *rand.Rand, size int) reflect.Value {
	numNodes := rand.Int()%100 + 2
	// number of tags is in range [2, numNodes]
	numTags := rand.Int()%(numNodes-1) + 2

	params := VbmapParams{
		Tags:        equalTags(numNodes, numTags),
		NumNodes:    numNodes,
		NumSlaves:   10,
		NumVBuckets: 1024,
		NumReplicas: 1,
	}
	normalizeParams(&params)

	return reflect.ValueOf(EqualTagsR1VbmapParams{params})
}
Example #20
0
File: quick.go Project: achanda/go
// randFloat32 generates a random float taking the full range of a float32.
func randFloat32(rand *rand.Rand) float32 {
	f := rand.Float64() * math.MaxFloat32
	if rand.Int()&1 == 1 {
		f = -f
	}
	return float32(f)
}
func BenchmarkPriorityQueue_int(b *testing.B) {
	pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
		return x.(int) < y.(int)
	}), BENCH_Q_SIZE)
	var src rand.Source = rand.NewSource(0)
	var r *rand.Rand = rand.New(src)
	for n := 0; n < b.N; n++ {
		//Populate the priority queue with random ints
		for i := 0; i < BENCH_Q_SIZE; i++ {
			pq.Insert(r.Int())
		}
		//Empty the priority queue
		for i := 0; i < BENCH_Q_SIZE; i++ {
			pq.PopTop()
		}
	}
}
func BenchmarkPriorityQueue_Time(b *testing.B) {
	pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
		return x.(time.Time).Before(y.(time.Time))
	}), BENCH_Q_SIZE)
	var src rand.Source = rand.NewSource(0)
	var r *rand.Rand = rand.New(src)
	for n := 0; n < b.N; n++ {
		//Populate the priority queue with random ints
		for i := 0; i < BENCH_Q_SIZE; i++ {
			pq.Insert(time.Now().Add(time.Hour * time.Duration(r.Int())))
		}
		//Empty the priority queue
		for i := 0; i < BENCH_Q_SIZE; i++ {
			pq.PopTop()
		}
	}
}
Example #23
0
func (c Publishing) Generate(r *rand.Rand, _ int) reflect.Value {
	var ok bool
	var t reflect.Value

	p := Publishing{}
	//p.DeliveryMode = uint8(r.Intn(3))
	//p.Priority = uint8(r.Intn(8))

	if r.Intn(2) > 0 {
		p.ContentType = "application/octet-stream"
	}

	if r.Intn(2) > 0 {
		p.ContentEncoding = "gzip"
	}

	if r.Intn(2) > 0 {
		p.CorrelationId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.ReplyTo = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.MessageId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.Type = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.AppId = fmt.Sprintf("%d", r.Int())
	}

	if r.Intn(2) > 0 {
		p.Timestamp = time.Unix(r.Int63(), r.Int63())
	}

	if t, ok = quick.Value(reflect.TypeOf(p.Body), r); ok {
		p.Body = t.Bytes()
	}

	return reflect.ValueOf(p)
}
Example #24
0
func MutateEqn_Vanilla(eqn Expr, egp *prob.TreeParams, rng *rand.Rand, sysvals []float64) {
	mut := false
	for !mut {
		s2 := rng.Intn(eqn.Size())
		s2_tmp := s2
		e2 := eqn.GetExpr(&s2_tmp)

		t := e2.ExprType()

		switch t {
		case CONSTANTF:
			if egp.NumSys == 0 {
				e2.(*ConstantF).F += rng.NormFloat64()
			} else {
				// mod coeff
				if rng.Intn(2) == 0 {
					e2.(*ConstantF).F += rng.NormFloat64()
				} else {
					// coeff -> system
					var mul Mul
					s := &System{P: rng.Intn(egp.NumSys)}
					e2.(*ConstantF).F /= sysvals[s.P]
					mul.CS[0] = s
					mul.CS[1] = e2.(*ConstantF)
					e2 = &mul
				}
			}
			mut = true
		case SYSTEM:
			e2.(*System).P = rng.Int() % egp.NumSys
			mut = true
		case VAR:
			p := egp.UsableVars[rng.Intn(len(egp.UsableVars))]
			e2.(*Var).P = p

			// r := rng.Intn(len(egp.UsableVars))
			// e2.(*Var).P = egp.UsableVars[r]
			mut = true
		case ADD:

		case MUL:

		}
	}
}
Example #25
0
func NewRandomSimilarityProvider(r *rand.Rand) *RandomSimilarityProvider {
	sims := make([]Similarity, len(allSims))
	for i, v := range r.Perm(len(allSims)) {
		sims[i] = allSims[v]
		assert(sims[i] != nil)
	}
	ans := &RandomSimilarityProvider{
		Locker:           &sync.Mutex{},
		defaultSim:       NewDefaultSimilarity(),
		previousMappings: make(map[string]Similarity),
		perFieldSeed:     r.Int(),
		coordType:        r.Intn(3),
		shouldQueryNorm:  r.Intn(2) == 0,
		knownSims:        sims,
	}
	ans.PerFieldSimilarityWrapper = NewPerFieldSimilarityWrapper(ans)
	return ans
}
Example #26
0
func main() {
	flag.Parse()

	//var r *rand.Rand = rand.New(rand.NewSource(time.Nanosecond))
	var r *rand.Rand = rand.New(rand.NewSource(int64(time.Now().Nanosecond())))

	for i := 0; i < *pwdCount; i++ {
		var pwd []byte = make([]byte, *pwdLength)

		for j := 0; j < *pwdLength; j++ {
			index := r.Int() % len(pwdCodes)

			pwd[j] = pwdCodes[index]
		}

		fmt.Printf("%s\r\n", string(pwd))
	}
}
Example #27
0
func NewRandomSimilarityProvider(r *rand.Rand) *RandomSimilarityProvider {
	sims := make([]Similarity, len(allSims))
	for i, v := range r.Perm(len(allSims)) {
		sims[i] = allSims[v]
	}
	return &RandomSimilarityProvider{
		PerFieldSimilarityWrapper: NewPerFieldSimilarityWrapper(func(name string) Similarity {
			panic("not implemented yet")
		}),
		Locker:           &sync.Mutex{},
		defaultSim:       NewDefaultSimilarity(),
		previousMappings: make(map[string]Similarity),
		perFieldSeed:     r.Int(),
		coordType:        r.Intn(3),
		shouldQueryNorm:  r.Intn(2) == 0,
		knownSims:        sims,
	}
}
Example #28
0
func randObject(rand *rand.Rand) Object {
	roll := rand.Int() % 32
	switch {
	case roll == 0:
		return Branch
	case roll < 8:
		return Commit
	default:
		return File
	}
}
Example #29
0
// randome fill the bytes.
func RandomFill(b []byte) {
	// fetch in buffered chan.
	var random *rand.Rand
	select {
	case random = <-randoms:
	default:
		random = rand.New(rand.NewSource(time.Now().UnixNano()))
	}

	// use the random.
	for i := 0; i < len(b); i++ {
		// the common value in [0x0f, 0xf0]
		b[i] = byte(0x0f + (random.Int() % (256 - 0x0f - 0x0f)))
	}

	// put back in buffered chan.
	select {
	case randoms <- random:
	default:
	}
}
Example #30
0
func (_ EqualTagsVbmapParams) Generate(rand *rand.Rand, size int) reflect.Value {
	numNodes := rand.Int()%100 + 2
	numReplicas := rand.Int()%3 + 1
	if numReplicas >= numNodes {
		numReplicas = numNodes - 1
	}

	// number of tags is in range [numReplicas+1, numNodes]
	numTags := rand.Int()%(numNodes-numReplicas) + numReplicas + 1

	params := VbmapParams{
		Tags:        equalTags(numNodes, numTags),
		NumNodes:    numNodes,
		NumSlaves:   10,
		NumVBuckets: 1024,
		NumReplicas: numReplicas,
	}
	normalizeParams(&params)

	return reflect.ValueOf(EqualTagsVbmapParams{params})
}