當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。