當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Rand.Perm方法代碼示例

本文整理匯總了Golang中math/rand.Rand.Perm方法的典型用法代碼示例。如果您正苦於以下問題:Golang Rand.Perm方法的具體用法?Golang Rand.Perm怎麽用?Golang Rand.Perm使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在math/rand.Rand的用法示例。


在下文中一共展示了Rand.Perm方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: New

func New(r *rand.Rand) *Simplex {
	var s Simplex
	perm := r.Perm(256)
	copy(s[:256], perm)
	copy(s[256:], perm)
	return &s
}
開發者ID:BenLubar,項目名稱:untitled-game,代碼行數:7,代碼來源:simplex.go

示例2: shuffleStrings

// Shuffle a slice of strings.
func shuffleStrings(strings []string, rng *rand.Rand) []string {
	var shuffled = make([]string, len(strings))
	for i, j := range rng.Perm(len(strings)) {
		shuffled[j] = strings[i]
	}
	return shuffled
}
開發者ID:MaxHalford,項目名稱:gago,代碼行數:8,代碼來源:util.go

示例3: DepthFirstRandom

// DepthFirstRandom traverses a graph depth first, but following arcs in
// random order among arcs from a single node.
//
// If Rand r is nil, the method creates a new source and generator for
// one-time use.
//
// Usage is otherwise like the DepthFirst method.  See DepthFirst.
//
// There are equivalent labeled and unlabeled versions of this method.
func (g AdjacencyList) DepthFirstRandom(start NI, bm *Bits, v OkNodeVisitor, r *rand.Rand) (ok bool) {
	if bm == nil {
		if v == nil {
			return false
		}
		bm = &Bits{}
	}
	if r == nil {
		r = rand.New(rand.NewSource(time.Now().UnixNano()))
	}
	var df func(n NI) bool
	df = func(n NI) bool {
		if bm.Bit(n) == 1 {
			return true
		}
		bm.SetBit(n, 1)
		if v != nil && !v(n) {
			return false
		}
		to := g[n]
		for _, i := range r.Perm(len(to)) {
			if !df(to[i]) {
				return false
			}
		}
		return true
	}
	return df(start)
}
開發者ID:RandomArray,項目名稱:gdrive,代碼行數:38,代碼來源:adj_RO.go

示例4: generateInput

func generateInput(rnd *rand.Rand, size int) []byte {
	permutations := rnd.Perm(size)
	data := make([]byte, size)
	for i, p := range permutations {
		data[i] = letters[p%len(letters)]
	}
	return data
}
開發者ID:skhal,項目名稱:goplayground,代碼行數:8,代碼來源:fastrndpipeline.go

示例5: shuffleEndpoints

func shuffleEndpoints(r *rand.Rand, eps []url.URL) []url.URL {
	p := r.Perm(len(eps))
	neps := make([]url.URL, len(eps))
	for i, k := range p {
		neps[i] = eps[k]
	}
	return neps
}
開發者ID:jak-atx,項目名稱:vic,代碼行數:8,代碼來源:client.go

示例6: shuffle

func (collection *exampleCollection) shuffle(r *rand.Rand) {
	sort.Sort(collection)
	permutation := r.Perm(len(collection.examples))
	shuffledExamples := make([]*example, len(collection.examples))
	for i, j := range permutation {
		shuffledExamples[i] = collection.examples[j]
	}
	collection.examples = shuffledExamples
}
開發者ID:khwang1,項目名稱:cf-acceptance-tests,代碼行數:9,代碼來源:example_collection.go

示例7: Shuffle

func (e *Specs) Shuffle(r *rand.Rand) {
	sort.Sort(e)
	permutation := r.Perm(len(e.specs))
	shuffledSpecs := make([]*Spec, len(e.specs))
	for i, j := range permutation {
		shuffledSpecs[i] = e.specs[j]
	}
	e.specs = shuffledSpecs
}
開發者ID:BlueSpice,項目名稱:cli,代碼行數:9,代碼來源:specs.go

示例8: shuffle

func (container *containerNode) shuffle(r *rand.Rand) {
	sort.Sort(container)
	permutation := r.Perm(len(container.subjectAndContainerNodes))
	shuffledNodes := make([]node, len(container.subjectAndContainerNodes))
	for i, j := range permutation {
		shuffledNodes[i] = container.subjectAndContainerNodes[j]
	}
	container.subjectAndContainerNodes = shuffledNodes
}
開發者ID:nikai3d,項目名稱:ginkgo,代碼行數:9,代碼來源:container_node.go

示例9: sample

// Sample n unique individuals from a slice of individuals
func (indis Individuals) sample(n int, generator *rand.Rand) ([]int, Individuals) {
	var (
		sample  = make(Individuals, n)
		indexes = generator.Perm(len(indis))[:n]
	)
	for i, j := range indexes {
		sample[i] = indis[j]
	}
	return indexes, sample
}
開發者ID:tomzhang,項目名稱:gago,代碼行數:11,代碼來源:individual.go

示例10: ShuffledIndex

func ShuffledIndex(rnd *rand.Rand, n, round int, fn func(i int)) {
	if rnd == nil {
		rnd = NewRand()
	}
	for x := 0; x < round; x++ {
		for _, i := range rnd.Perm(n) {
			fn(i)
		}
	}
	return
}
開發者ID:zangfong,項目名稱:goleveldb,代碼行數:11,代碼來源:util.go

示例11: BreadthFirst

// BreadthFirst traverses a directed or undirected graph in breadth first order.
//
// Argument start is the start node for the traversal.  If r is nil, nodes are
// visited in deterministic order.  If a random number generator is supplied,
// nodes at each level are visited in random order.
//
// Argument f can be nil if you have no interest in the FromList path result.
// If FromList f is non-nil, the method populates f.Paths and sets f.MaxLen.
// It does not set f.Leaves.  For convenience argument f can be a zero value
// FromList.  If f.Paths is nil, the FromList is initialized first.  If f.Paths
// is non-nil however, the FromList is  used as is.  The method uses a value of
// PathEnd.Len == 0 to indentify unvisited nodes.  Existing non-zero values
// will limit the traversal.
//
// Traversal calls the visitor function v for each node starting with node
// start.  If v returns true, traversal continues.  If v returns false, the
// traversal terminates immediately.  PathEnd Len and From values are updated
// before calling the visitor function.
//
// On return f.Paths and f.MaxLen are set but not f.Leaves.
//
// Returned is the number of nodes visited and ok = true if the traversal
// ran to completion or ok = false if it was terminated by the visitor
// function returning false.
//
// There are equivalent labeled and unlabeled versions of this method.
func (g AdjacencyList) BreadthFirst(start NI, r *rand.Rand, f *FromList, v OkNodeVisitor) (visited int, ok bool) {
	switch {
	case f == nil:
		e := NewFromList(len(g))
		f = &e
	case f.Paths == nil:
		*f = NewFromList(len(g))
	}
	rp := f.Paths
	// the frontier consists of nodes all at the same level
	frontier := []NI{start}
	level := 1
	// assign path when node is put on frontier,
	rp[start] = PathEnd{Len: level, From: -1}
	for {
		f.MaxLen = level
		level++
		var next []NI
		if r == nil {
			for _, n := range frontier {
				visited++
				if !v(n) { // visit nodes as they come off frontier
					return
				}
				for _, nb := range g[n] {
					if rp[nb].Len == 0 {
						next = append(next, nb)
						rp[nb] = PathEnd{From: n, Len: level}
					}
				}
			}
		} else { // take nodes off frontier at random
			for _, i := range r.Perm(len(frontier)) {
				n := frontier[i]
				// remainder of block same as above
				visited++
				if !v(n) {
					return
				}
				for _, nb := range g[n] {
					if rp[nb].Len == 0 {
						next = append(next, nb)
						rp[nb] = PathEnd{From: n, Len: level}
					}
				}
			}
		}
		if len(next) == 0 {
			break
		}
		frontier = next
	}
	return visited, true
}
開發者ID:RandomArray,項目名稱:gdrive,代碼行數:80,代碼來源:adj_RO.go

示例12: Apply

// Apply permutation mutation.
func (mut MutPermute) Apply(indi *Individual, generator *rand.Rand) {
	for i := 0; i <= generator.Intn(mut.Max); i++ {
		// Choose two points on the genome
		var (
			points = generator.Perm(len(indi.Genome))[:2]
			i      = points[0]
			j      = points[1]
		)
		// Permute the genes
		indi.Genome[i], indi.Genome[j] = indi.Genome[j], indi.Genome[i]
	}
}
開發者ID:tomzhang,項目名稱:gago,代碼行數:13,代碼來源:mutation.go

示例13: genValues

// genValues creates a slice containing a random number of random values
// that when scaled by adding minValue will fall into [min, max].
func (w *WeightedDist) genValues(rng *rand.Rand) {
	nValues := (w.maxValue + 1) - w.minValue
	values := rng.Perm(nValues)
	if nValues < minValues {
		nValues = minValues
	}
	if nValues > maxValues {
		nValues = maxValues
	}
	nValues = rng.Intn(nValues) + 1
	w.values = values[:nValues]
}
開發者ID:OperatorFoundation,項目名稱:obfs4,代碼行數:14,代碼來源:weighted_dist.go

示例14: selectRandom

// selectRandom chooses up to count random store descriptors from the given
// store list.
func selectRandom(randGen *rand.Rand, count int, sl StoreList) []*roachpb.StoreDescriptor {
	var descs []*roachpb.StoreDescriptor
	// Randomly permute available stores matching the required attributes.
	for _, idx := range randGen.Perm(len(sl.stores)) {
		// Add this store; exit loop if we've satisfied count.
		descs = append(descs, sl.stores[idx])
		if len(descs) >= count {
			break
		}
	}
	if len(descs) == 0 {
		return nil
	}
	return descs
}
開發者ID:nporsche,項目名稱:cockroach,代碼行數:17,代碼來源:balancer.go

示例15: buildShuffled

func (h *dbCorruptHarness) buildShuffled(n int, rnd *rand.Rand) {
	p := &h.dbHarness
	t := p.t
	db := p.db

	batch := new(Batch)
	for i := range rnd.Perm(n) {
		batch.Reset()
		batch.Put(tkey(i), tval(i, ctValSize))
		err := db.Write(batch, p.wo)
		if err != nil {
			t.Fatal("write error: ", err)
		}
	}
}
開發者ID:stoiclabs,項目名稱:blockchainr,代碼行數:15,代碼來源:corrupt_test.go


注:本文中的math/rand.Rand.Perm方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。