当前位置: 首页>>代码示例>>Golang>>正文


Golang rand.Perm函数代码示例

本文整理汇总了Golang中math/rand.Perm函数的典型用法代码示例。如果您正苦于以下问题:Golang Perm函数的具体用法?Golang Perm怎么用?Golang Perm使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Perm函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: TestMap

func TestMap(t *testing.T) {
	func() {
		d := newMapString()
		ix := rand.Perm(testN)
		for _, v := range testVals {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testVals[idx].id)
		}
		for _, idx := range ix {
			if d.exist(testVals[idx].id) {
				t.Errorf("%s should have not existed!", testVals[idx].id)
			}
		}
	}()

	func() {
		d := newMapPointer()
		ix := rand.Perm(testN)
		for _, v := range testVals {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testVals[idx].id)
		}
		for _, idx := range ix {
			if d.exist(testVals[idx].id) {
				t.Errorf("%s should have not existed!", testVals[idx].id)
			}
		}
	}()
}
开发者ID:xqbumu,项目名称:learn,代码行数:33,代码来源:map_string_vs_map_pointer_test.go

示例2: TestInterface

func TestInterface(t *testing.T) {
	func() {
		d := newDataRWMutex()
		ix := rand.Perm(testSize)
		for _, v := range testValues {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testValues[idx])
		}
		for _, idx := range ix {
			if d.exist(testValues[idx]) {
				t.Errorf("%s should have not existed!", testValues[idx])
			}
		}
	}()

	func() {
		d := newDataMutex()
		ix := rand.Perm(testSize)
		for _, v := range testValues {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testValues[idx])
		}
		for _, idx := range ix {
			if d.exist(testValues[idx]) {
				t.Errorf("%s should have not existed!", testValues[idx])
			}
		}
	}()
}
开发者ID:xqbumu,项目名称:learn,代码行数:33,代码来源:rwmutex_vs_mutex_test.go

示例3: advanceState

func advanceState(c appengine.Context, h *hunt.Hunt, currentState int) {
	err := datastore.RunInTransaction(c, func(c appengine.Context) error {
		h := hunt.ID(c, h.ID)
		if h == nil || h.State != currentState {
			// TODO(dneal): Return a real error.
			return nil
		}
		switch h.State {
		case hunt.StatePreLaunch:
			teams := team.All(c, h)
			nonPaperOrder := rand.Perm(len(teams))
			paperOrder := rand.Perm(len(teams))
			for i := range teams {
				puzzle.New(c, h, teams[i], nonPaperOrder[i]+1, false)
				puzzle.New(c, h, teams[i], len(teams)+paperOrder[i]+1, true)
			}
		case hunt.StateSurveying:
			tally.BuildFinalTally(c, h)
		}
		h.State++
		h.Write(c)
		broadcast.SendRefresh(c, h)
		return nil
	}, nil)
	if err != nil {
		c.Errorf("Error: %v", err)
	}
}
开发者ID:offpath,项目名称:ironpuzzler,代码行数:28,代码来源:api.go

示例4: TestEdgeX

func TestEdgeX(t *testing.T) {
	mom := rand.Perm(8)
	dad := rand.Perm(8)
	child := make([]int, 8)
	perm.EdgeX(child, mom, dad)
	validate(t, child)
}
开发者ID:lucmichalski,项目名称:evo,代码行数:7,代码来源:perm_test.go

示例5: assignSpecialCharacters

func (ag *AvalonGame) assignSpecialCharacters() {
	// === Good ===
	randomOrder := rand.Perm(ag.NumGoods())
	// Merlin
	ag.SpecialCharacters["merlin"] = ag.GoodCharacters[randomOrder[0]]
	// Percival
	if ag.OptionEnabled[OptionMorganaPercival] {
		ag.SpecialCharacters["percival"] = ag.GoodCharacters[randomOrder[1]]
	}

	// === Evil ===
	randIndex := 1
	randomOrder = rand.Perm(ag.NumEvils())
	// Assassin
	ag.SpecialCharacters["assassin"] = ag.EvilCharacters[randomOrder[0]]
	// Mordred
	if ag.OptionEnabled[OptionMordred] {
		ag.SpecialCharacters["mordred"] = ag.EvilCharacters[randomOrder[randIndex]]
		randIndex++
	}
	// Morgana
	if ag.OptionEnabled[OptionMorganaPercival] {
		ag.SpecialCharacters["morgana"] = ag.EvilCharacters[randomOrder[randIndex]]
		randIndex++
	}
	// Oberon
	if ag.OptionEnabled[OptionOberon] {
		ag.SpecialCharacters["oberon"] = ag.EvilCharacters[randomOrder[randIndex]]
		randIndex++
	}
}
开发者ID:justinkim,项目名称:avalon,代码行数:31,代码来源:prev_ag.go

示例6: TestBinaryTreeFind

func TestBinaryTreeFind(t *testing.T) {
	const count = 100
	for i := 0; i < 10; i++ {
		tree := Tree{Compare: func(a, b interface{}) ComparisonResult {
			aa := a.(int)
			bb := b.(int)
			switch {
			case aa < bb:
				return Less
			case aa > bb:
				return Greater
			default:
				return Equal
			}
		}}
		list := rand.Perm(count)
		for _, j := range list {
			if e := tree.Add(j); e != nil {
				t.Error(e)
			}
		}
		list = rand.Perm(count)
		for _, j := range list {
			if _, _, n := tree.Find(j); n == nil {
				t.Errorf("Should have found %d, but didn't", j)
			} else if v, ok := n.Data.(int); !ok {
				t.Errorf("Unable to cast data to int... %+v", n.Data)
			} else if v != j {
				t.Errorf("Expected to find %d, but got %d", j, v)
			}
		}
	}
}
开发者ID:quarnster,项目名称:util,代码行数:33,代码来源:binarytree_test.go

示例7: TestMapTo

func TestMapTo(t *testing.T) {
	func() {
		d := newSlice()
		ix := rand.Perm(testN)
		for _, v := range testVals {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testVals[idx])
		}
		for _, idx := range ix {
			if d.exist(testVals[idx]) {
				t.Errorf("%s should have not existed!", testVals[idx])
			}
		}
	}()

	func() {
		d := newMap()
		ix := rand.Perm(testN)
		for _, v := range testVals {
			d.set(v)
		}
		for _, idx := range ix {
			d.delete(testVals[idx])
		}
		for _, idx := range ix {
			if d.exist(testVals[idx]) {
				t.Errorf("%s should have not existed!", testVals[idx])
			}
		}
	}()
}
开发者ID:xqbumu,项目名称:learn,代码行数:33,代码来源:slice_vs_map_test.go

示例8: genRandomGenSparseVectorInt

func genRandomGenSparseVectorInt(m int) *GenSparseVector {
	l := m * 3 / 4
	values := make([]Value, l)
	for i, v := range rand.Perm(m)[:l] {
		values[i] = Value(v)
	}
	return NewGenSparseVector(IntIndex(rand.Perm(m)[:l]), values)
}
开发者ID:philpearl,项目名称:sparsevector,代码行数:8,代码来源:gensparsevector_test.go

示例9: doStuff

func doStuff(id int,
	res chan<- Result,
	wg *sync.WaitGroup,
	body []byte,
	prot, addr string) {

	defer wg.Done()

	localstats := [256]int64{}

	keys := make([][]byte, *nkeys / *concurrency)
	vbuckets := make([]uint16, *nvbuckets)
	for i := 0; i < len(keys); i++ {
		keys[i] = []byte(fmt.Sprintf("c%d.k%d", id, i))
		vbuckets = append(vbuckets, uint16(rand.Intn(*nvbuckets)))
	}

	client, err := memcached.Connect(prot, addr)
	if err != nil {
		log.Printf("Error connecting to %v/%v: %v", prot, addr, err)
		return
	}

	go handleResponses(client, res)

	applyLocalStats := func() {
		for j, v := range localstats {
			atomic.AddInt64(&stats[j], v)
			localstats[j] = 0
		}
	}

	cmdi := 0
	ids := rand.Perm(len(keys))
	for {
		for i, thisId := range ids {
			key := keys[thisId]
			opcode := cmds.cmds[cmdi]

			sendCommand(client, opcode, vbuckets[thisId], key, body)
			localstats[opcode]++

			if i%1000 == 0 {
				applyLocalStats()
				if atomic.LoadInt32(&isDone) == 1 {
					return
				}
			}
		}
		applyLocalStats()
		cmdi++
		if cmdi >= len(cmds.cmds) {
			cmdi = 0
			ids = rand.Perm(len(keys))
		}
	}
}
开发者ID:dustin,项目名称:gohammer,代码行数:57,代码来源:gohammer.go

示例10: RandomCubieCube

func RandomCubieCube() CubieCube {
	var res CubieCube

	// Generate a random permutation for the corners.
	pieces := rand.Perm(8)
	for i, x := range pieces {
		res.Corners[i].Piece = x
	}

	// Generate a random permutation for the edges.
	cornerParity := parity(pieces)
	pieces = rand.Perm(12)
	for i, x := range pieces {
		res.Edges[i].Piece = x
	}

	// Make sure the overall parity is even.
	if cornerParity != parity(pieces) {
		res.Edges[11], res.Edges[10] = res.Edges[10], res.Edges[11]
	}

	// Generate edge orientations.
	lastFlip := false
	for i := 0; i < 11; i++ {
		if rand.Intn(2) == 0 {
			lastFlip = !lastFlip
			res.Edges[i].Flip = true
		}
	}
	res.Edges[11].Flip = lastFlip

	// Generate the corner orientations.
	for i := 0; i < 7; i++ {
		res.Corners[i].Orientation = rand.Intn(3)
	}
	var orientations [8]int
	for i, x := range []int{0, 1, 5, 4, 6, 2, 3, 7} {
		orientations[i] = res.Corners[x].Orientation
	}
	for i := 0; i < 7; i++ {
		thisOrientation := orientations[i]
		nextOrientation := orientations[i+1]
		if thisOrientation == 2 {
			orientations[i+1] = (nextOrientation + 2) % 3
		} else if thisOrientation == 0 {
			orientations[i+1] = (nextOrientation + 1) % 3
		}
	}
	if orientations[7] == 0 {
		res.Corners[7].Orientation = 2
	} else if orientations[7] == 2 {
		res.Corners[7].Orientation = 0
	}

	return res
}
开发者ID:chai2010,项目名称:gocube,代码行数:56,代码来源:random_state.go

示例11: BenchmarkFind

func BenchmarkFind(b *testing.B) {
	bst := new(T)
	for _, i := range rand.Perm(b.N) {
		bst.Insert(i, i)
	}

	b.ResetTimer()
	for _, i := range rand.Perm(b.N) {
		bst.Find(i)
	}
}
开发者ID:emanoelxavier,项目名称:collections-cosn,代码行数:11,代码来源:bst_test.go

示例12: genRandomSparseVector

func genRandomSparseVector(m int) *SparseVectorUint32 {
	l := m * 3 / 4
	values := make([]Value, l)
	for i, v := range rand.Perm(m)[:l] {
		values[i] = Value(v)
	}
	index := make([]uint32, l)
	for i, v := range rand.Perm(m)[:l] {
		index[i] = uint32(v)
	}
	return NewSparseVectorUint32(index, values)
}
开发者ID:philpearl,项目名称:sparsevector,代码行数:12,代码来源:sparsevector_test.go

示例13: main

func main() {
	rand.Seed(time.Now().UnixNano())

	indices := rand.Perm(len(SOURCE))
	var output string
	for i := 0; i < 50; i++ {
		if i != 0 && i%7 == 0 {
			indices = rand.Perm(len(SOURCE))
		}
		output += fmt.Sprintf("%c", SOURCE[indices[i%7]])
	}
	fmt.Println(output)
}
开发者ID:evanh,项目名称:dailyprogrammer,代码行数:13,代码来源:randombagsimple.go

示例14: openReplicaRole

func (r *roler) openReplicaRole(shardToMasterAddress map[int]route.Address, shardToReplicaAddress map[int]map[int]route.Address) (int, int, bool) {
	for _, shard := range rand.Perm(r.sharder.NumShards()) {
		addresses := shardToReplicaAddress[shard]
		if len(addresses) < r.numReplicas && !r.hasRoleForShard(shard, shardToMasterAddress, shardToReplicaAddress) {
			for _, index := range rand.Perm(r.numReplicas) {
				if _, ok := addresses[index]; !ok {
					return shard, index, true
				}
			}
		}
	}
	return 0, 0, false
}
开发者ID:kunthar,项目名称:pachyderm,代码行数:13,代码来源:roler.go

示例15: genRandomGenSparseVectorUint32

func genRandomGenSparseVectorUint32(m int) *GenSparseVector {
	l := m * 3 / 4
	values := make([]Value, l)
	for i, v := range rand.Perm(m)[:l] {
		values[i] = Value(v)
	}
	index := make(Uint32Index, l)
	for i, v := range rand.Perm(m)[:l] {
		index[i] = uint32(v)
	}

	return NewGenSparseVector(index, values)
}
开发者ID:philpearl,项目名称:sparsevector,代码行数:13,代码来源:gensparsevector_test.go


注:本文中的math/rand.Perm函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。