本文整理汇总了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
}
示例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
}
示例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)
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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]
}
}
示例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]
}
示例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
}
示例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)
}
}
}