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


Golang rand.Perm函数代码示例

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


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

示例1: TestRandomReplace

func TestRandomReplace(t *testing.T) {
        tree := New(IntLess)
        n := 100
        perm := rand.Perm(n)
        for i := 0; i < n; i++ {
                tree.ReplaceOrInsert(perm[i])
        }
        perm = rand.Perm(n)
        for i := 0; i < n; i++ {
                if replaced := tree.ReplaceOrInsert(perm[i]); replaced == nil || replaced.(int) != perm[i] {

                        t.Errorf("error replacing")
                }
        }
}
开发者ID:patrickxb,项目名称:GoLLRB,代码行数:15,代码来源:llrb_test.go

示例2: main

func main() {
	rand.Seed(time.Seconds())
	nums := rand.Perm(MAX)[:COUNT]
	for _, val := range nums {
		fmt.Println(val)
	}
}
开发者ID:dhconnelly,项目名称:abandoned,代码行数:7,代码来源:prob4.go

示例3: New

// New returns a new, random binary tree holding the values k, 2k, ..., 10k.
func New(k int) *Tree {
	var t *Tree
	for _, v := range rand.Perm(10) {
		t = insert(t, (1+v)*k)
	}
	return t
}
开发者ID:hambith,项目名称:Golang-spanish-documentation,代码行数:8,代码来源:tree.go

示例4: main

func main() {
	n := 10000
	vrand := rand.Perm(n)
	vinc := make([]int, n)
	for i := range vinc {
		vinc[i] = i
	}
	vdec := make([]int, n)
	for i := range vdec {
		vdec[i] = i
	}

	if !CheckInsertsAndDeletes(vrand) {
		fmt.Printf("Failed inserts and deletes when done in random order.\n")
	}
	if !CheckQueries(vrand) {
		fmt.Printf("Failed queries when done in random order.\n")
	}

	if !CheckInsertsAndDeletes(vinc) {
		fmt.Printf("Failed inserts and deletes when done in increasing order.\n")
	}
	if !CheckQueries(vinc) {
		fmt.Printf("Failed queries when done in increasing order.\n")
	}

	if !CheckInsertsAndDeletes(vdec) {
		fmt.Printf("Failed inserts and deletes when done in decreasing order.\n")
	}
	if !CheckQueries(vdec) {
		fmt.Printf("Failed queries when done in decreasing order.\n")
	}
}
开发者ID:runningwild,项目名称:go-btree,代码行数:33,代码来源:test.go

示例5: TestRandomInsertDeleteNonExistent

func TestRandomInsertDeleteNonExistent(t *testing.T) {
        tree := New(IntLess)
        n := 100
        perm := rand.Perm(n)
        for i := 0; i < n; i++ {
                tree.ReplaceOrInsert(perm[i])
        }
        if tree.Delete(200) != nil {
                t.Errorf("deleted non-existent item")
        }
        if tree.Delete(-2) != nil {
                t.Errorf("deleted non-existent item")
        }
        for i := 0; i < n; i++ {
                if u := tree.Delete(i); u == nil || u.(int) != i {
                        t.Errorf("delete failed")
                }
        }
        if tree.Delete(200) != nil {
                t.Errorf("deleted non-existent item")
        }
        if tree.Delete(-2) != nil {
                t.Errorf("deleted non-existent item")
        }
}
开发者ID:patrickxb,项目名称:GoLLRB,代码行数:25,代码来源:llrb_test.go

示例6: main

func main() {
	names := []string{
		"Unique Inserts",
		"Repeated Inserts",
		"Unique Deletes",
		"Repeated Deletes",
		"Queries",
	}

	d := rand.Perm(N)
	total := Bench(d)
	for i := 1; i < R; i++ {
		times := Bench(d)
		for j := range times {
			total[j] += times[j]
		}
	}
	for i := range total {
		total[i] /= float(R)
	}

	fmt.Printf("Using input size %d and averaged over %d runs.\n", N, R)
	fmt.Printf("%3.3f:\t%d\t%s\n", total[0], N, names[0])
	fmt.Printf("%3.3f:\t%d\t%s\n", total[1], N, names[1])
	fmt.Printf("%3.3f:\t%d\t%s\n", total[2], N/2, names[2])
	fmt.Printf("%3.3f:\t%d\t%s\n", total[3], N/2, names[3])
	fmt.Printf("%3.3f:\t%d\t%s\n", total[4], N, names[4])
}
开发者ID:runningwild,项目名称:go-btree,代码行数:28,代码来源:bench.go

示例7: randomString

func randomString() string {
	alphabets := "abcdefghijklmnopqrstuvwxyz"
	intArray := rand.Perm(len(alphabets))
	rstring := strings.Map(func(i int) int { return int(alphabets[intArray[i%26]]) },
		alphabets)
	return rstring
}
开发者ID:sunilnandihalli,项目名称:chatserver-in-go,代码行数:7,代码来源:badClient.go

示例8: TestHashTree1

// This test checks that the hash tree is always the same
// no matter in which order the IDs are being inserted
func TestHashTree1(t *testing.T) {
	set := []string{}
	for i := 0; i < 1000; i++ {
		set = append(set, fmt.Sprintf("m%v", i))
	}
	hash := ""
	for test := 0; test < 1000; test++ {
		tree := NewSimpleHashTree()
		perm := rand.Perm(len(set))
		for i := 0; i < len(set); i++ {
			member := set[perm[i]]
			h := sha256.New()
			h.Write([]byte(member))
			tree.Add(hex.EncodeToString(h.Sum()))
		}
		result := tree.Hash()
		if test == 0 {
			hash = result
		} else {
			if hash != result {
				t.Fatal("Hashes are not the same")
			}
		}
	}
}
开发者ID:AaronO,项目名称:lightwave,代码行数:27,代码来源:hashtree_test.go

示例9: main

func main() {
	runtime.GOMAXPROCS(12) //max number of processes for concurrency
	for i := 1; i < 7; i++ {
		done := make(chan sort.Algorithm) //channel for communication
		nums := rand.Perm(1000 * i)
		println("\nusing", 1*graph.Pow(10, i), "elements")
		nums1, nums2 := make([]int, len(nums)), make([]int, len(nums))
		copy(nums1, nums) //create copies of the array for others to sort
		copy(nums2, nums)
		go sort.TimeEvent(sort.InsertionSort, nums, "Insertion Sort", done)
		go sort.TimeEvent(sort.QuickSort, nums1, "Quick Sort", done)
		go sort.TimeEvent(sort.TreeSort, nums2, "Tree Sort", done)
		n := 0
		for alg := range done { //wait for the events to finish execution
			println(alg.Name, "finished in", alg.Time)
			switch alg.Name {
			case "Insertion Sort":
				insertionVals.Y[i] = alg.Time
			case "Quick Sort":
				quickVals.Y[i] = alg.Time
			case "Tree Sort":
				treeVals.Y[i] = alg.Time
			}
			n++
			if n > 2 {
				close(done)
			} //close the channel
		}
	}
	values := []graph.Values{treeVals, insertionVals, quickVals}
	graph.DrawToFile("output.svg", graph.NewGraph(100, 100, values))
}
开发者ID:abiosoft,项目名称:structures,代码行数:32,代码来源:structures.go

示例10: generate

func generate() *guess {
	rand.Seed(time.Nanoseconds())
	rnd := rand.Perm(10)
	tmp := new(guess)
	for i := range tmp.nmbr {
		tmp.nmbr[i] = rnd[i]
	}
	return tmp
}
开发者ID:kicool,项目名称:kicool.go,代码行数:9,代码来源:gnmb.go

示例11: BenchmarkDelete

func BenchmarkDelete(b *testing.B) {
	b.StopTimer()
	ints := rand.Perm(b.N)
	tree := treeOfInts(ints)

	b.StartTimer()
	for i := 0; i < b.N; i++ {
		tree.Delete(i)
	}
}
开发者ID:jeffallen,项目名称:treap,代码行数:10,代码来源:treap_test.go

示例12: TestRandomInsertSequentialDelete

func TestRandomInsertSequentialDelete(t *testing.T) {
        tree := New(IntLess)
        n := 1000
        perm := rand.Perm(n)
        for i := 0; i < n; i++ {
                tree.ReplaceOrInsert(perm[i])
        }
        for i := 0; i < n; i++ {
                tree.Delete(i)
        }
}
开发者ID:patrickxb,项目名称:GoLLRB,代码行数:11,代码来源:llrb_test.go

示例13: BenchmarkLookup

func BenchmarkLookup(b *testing.B) {
	b.StopTimer()
	ints := rand.Perm(b.N)
	tree := treeOfInts(ints)
	b.StartTimer()
	for j := 0; j < 10; j++ {
		for i := 0; i < len(ints)/10; i++ {
			_ = tree.Exists(ints[i])
		}
	}
}
开发者ID:jeffallen,项目名称:treap,代码行数:11,代码来源:treap_test.go

示例14: TestRandomInsertStats

func TestRandomInsertStats(t *testing.T) {
        tree := New(IntLess)
        n := 100000
        perm := rand.Perm(n)
        for i := 0; i < n; i++ {
                tree.ReplaceOrInsert(perm[i])
        }
        avg, _ := tree.HeightStats()
        expAvg := math.Log2(float64(n)) - 1.5
        if math.Fabs(avg-expAvg) >= 2.0 {
                t.Errorf("too much deviation from expected average height")
        }
}
开发者ID:patrickxb,项目名称:GoLLRB,代码行数:13,代码来源:llrb_test.go

示例15: TestRandomInsertOrder

func TestRandomInsertOrder(t *testing.T) {
        tree := New(IntLess)
        n := 1000
        perm := rand.Perm(n)
        for i := 0; i < n; i++ {
                tree.ReplaceOrInsert(perm[i])
        }
        c := tree.IterAscend()
        for j, item := 0, <-c; item != nil; j, item = j+1, <-c {
                if item.(int) != j {
                        t.Fatalf("bad order")
                }
        }
}
开发者ID:patrickxb,项目名称:GoLLRB,代码行数:14,代码来源:llrb_test.go


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