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


Golang Rand.Int31n方法代碼示例

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


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

示例1: DuplicateRequest

func DuplicateRequest(request *http.Request, r *rand.Rand) (request1 *http.Request, request2 *http.Request) {
	b1 := new(bytes.Buffer)
	b2 := new(bytes.Buffer)
	w := io.MultiWriter(b1, b2)
	io.Copy(w, request.Body)
	defer request.Body.Close()
	if r.Int31n(100) <= int32(*percent) {
		request1 = &http.Request{
			Method:        request.Method,
			URL:           request.URL,
			Proto:         "HTTP/1.1",
			ProtoMajor:    1,
			ProtoMinor:    1,
			Header:        request.Header,
			Body:          nopCloser{b1},
			Host:          request.Host,
			ContentLength: request.ContentLength,
		}
	} else {
		request1 = nil
	}
	request2 = &http.Request{
		Method:        request.Method,
		URL:           request.URL,
		Proto:         "HTTP/1.1",
		ProtoMajor:    1,
		ProtoMinor:    1,
		Header:        request.Header,
		Body:          nopCloser{b2},
		Host:          request.Host,
		ContentLength: request.ContentLength,
	}
	return
}
開發者ID:geidies,項目名稱:teeproxy,代碼行數:34,代碼來源:teeproxy.go

示例2: keyFromRand

func keyFromRand(r *rand.Rand) string {
	var key string
	for i := 0; i != 6; i++ {
		key += string(keyChars[r.Int31n(int32(len(keyChars)))])
	}
	return key
}
開發者ID:atombrella,項目名稱:souper,代碼行數:7,代碼來源:souperweb.go

示例3: MakePriority

// MakePriority generates a random priority value, biased by the
// specified userPriority. If userPriority=100, the resulting
// priority is 100x more likely to be probabilistically greater
// than a similar invocation with userPriority=1.
func MakePriority(r *rand.Rand, userPriority int32) int32 {
	// A currently undocumented feature allows an explicit priority to
	// be set by specifying priority < 1. The explicit priority is
	// simply -userPriority in this case. This is hacky, but currently
	// used for unittesting. Perhaps this should be documented and allowed.
	if userPriority < 0 {
		return -userPriority
	}
	if userPriority == 0 {
		userPriority = 1
	}
	// The idea here is to bias selection of a random priority from the
	// range [1, 2^31-1) such that if userPriority=100, it's 100x more
	// likely to be a higher int32 than if userPriority=1. The formula
	// below chooses random values according to the following table:
	//   userPriority  |  range
	//   1             |  all positive int32s
	//   10            |  top 9/10ths of positive int32s
	//   100           |  top 99/100ths of positive int32s
	//   1000          |  top 999/1000ths of positive int32s
	//   ...etc
	if r != nil {
		return math.MaxInt32 - r.Int31n(math.MaxInt32/userPriority)
	}
	return math.MaxInt32 - rand.Int31n(math.MaxInt32/userPriority)
}
開發者ID:Gardenya,項目名稱:cockroach,代碼行數:30,代碼來源:data.go

示例4: growTree

func growTree(chunk *chunk.Chunk, r *rand.Rand, x, y, z int32) {
	if x&0xF == x && z&0xF == z {
		return
	}
	if x&0xF < 2 || x&0xF > 13 || z&0xF < 2 || z&0xF > 13 {
		return
	}

	chunk.SetBlock(x, y-1, z, block.Dirt)

	height := r.Int31n(5) + 3
	for Y := y; Y < y+height; Y++ {
		chunk.SetBlock(x, Y, z, block.Log)
	}

	for X := x - 2; X <= x+2; X++ {
		for Z := z - 2; Z <= z+2; Z++ {
			for Y := y + height - 2; Y <= y+height+2; Y++ {
				if chunk.GetBlock(X, Y, Z) == block.Air {
					chunk.SetBlock(X, Y, Z, block.Leaves)
				}
			}
		}
	}
}
開發者ID:Nightgunner5,項目名稱:stuzzd,代碼行數:25,代碼來源:worldgen.go

示例5: rand_str

// this function inspired by http://devpy.wordpress.com/2013/10/24/create-random-string-in-golang/
func rand_str(str_size int, randSource *rand.Rand) string {
	alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
	var bytes = make([]byte, str_size)
	for i := 0; i < str_size; i++ {
		bytes[i] = alphanum[randSource.Int31n(int32(len(alphanum)))]
	}
	return string(bytes)
}
開發者ID:Tokutek,項目名稱:go-benchmark,代碼行數:9,代碼來源:iibench.go

示例6: shuffle

func shuffle(l []*Person, r *rand.Rand) {
	// First sort them so that they are in a deterministic order so that the random assignment can be re-encountered if needed
	sort.Sort(People(l))
	for i := int32(len(l) - 1); i > 0; i-- {
		j := r.Int31n(i + 1)
		l[i], l[j] = l[j], l[i]
	}
}
開發者ID:Carrotman42,項目名稱:secret-santa,代碼行數:8,代碼來源:secretsanta.go

示例7: generateA

func generateA(r *rand.Rand) *A {
	return &A{
		Name:     randString(r, 16),
		BirthDay: r.Int63(),
		Phone:    randString(r, 10),
		Siblings: r.Int31n(5),
		Spouse:   r.Intn(2) == 1,
		Money:    r.Float64(),
	}
}
開發者ID:zombiezen,項目名稱:go-capnproto2,代碼行數:10,代碼來源:bench_test.go

示例8: Permute

func Permute(v []int64, r *rand.Rand) {
	n := int32(len(v))

	for n > 0 {
		i := r.Int31n(n)
		aux := v[n-1]
		v[n-1] = v[i]
		v[i] = aux
		n--
	}
}
開發者ID:pombredanne,項目名稱:qlease,代碼行數:11,代碼來源:randperm.go

示例9: GenString

func GenString(template string, randSource *rand.Rand) string {
	var buf bytes.Buffer
	alpha := "abcdefghijklmnopqrstuvwxyz"
	nums := "0123456789"
	for i := 0; i < len(template); i++ {
		if template[i] == '#' {
			buf.WriteByte(nums[randSource.Int31n(int32(len(nums)))])
		} else if template[i] == '@' {
			buf.WriteByte(alpha[randSource.Int31n(int32(len(alpha)))])
		} else {
			buf.WriteByte(template[i])
		}
	}
	return buf.String()
}
開發者ID:Tokutek,項目名稱:go-benchmark,代碼行數:15,代碼來源:utils.go

示例10: createword

func createword(random *rand.Rand) string {
	x := getword("")
	s := ""

	for {
		x = x.next[random.Int31n(int32(len(x.next)))]

		if x.str == "" {
			return s
		}

		s += x.str + " "
	}

	return ""
}
開發者ID:bakks,項目名稱:fakehn,代碼行數:16,代碼來源:create.go

示例11: Generate

func (b bytesize) Generate(rand *rand.Rand, size int) reflect.Value {
	suffix := ""
	number := 1 + rand.Int31n(int32(size))
	chance := rand.Float32()
	if chance < 0.1 {
		suffix = "k"
	}
	// Can't test `kb`-style suffixes in darwin…
	return reflect.ValueOf(bytesize(fmt.Sprintf("%d%s", number, suffix)))
}
開發者ID:koenbollen,項目名稱:ddp,代碼行數:10,代碼來源:util_test.go

示例12: Numerify

func Numerify(pattern string, numerifyRune rune, rand *rand.Rand) string {
	result := []byte{}
	for _, char := range pattern {
		if char == numerifyRune {
			result = strconv.AppendInt(result, int64(rand.Int31n(10)), 10)
		} else {
			result = append(result, byte(char))
		}
	}

	return string(result)
}
開發者ID:venkatesh-ticketfly,項目名稱:gofaker,代碼行數:12,代碼來源:numerify.go

示例13: randomPoint

func randomPoint(r *rand.Rand) *pb.Point {
	lat := (r.Int31n(180) - 90) * 1e7
	long := (r.Int31n(360) - 180) * 1e7
	return &pb.Point{lat, long}
}
開發者ID:ikatson,項目名稱:etcd,代碼行數:5,代碼來源:client.go

示例14: rndValueType

// rndValueType returns random value of type xValueType
func rndValueType(rnd *rand.Rand) xValueType {
	return xValueType(rnd.Int31n(int32(xRnd + 1)))
}
開發者ID:christoffetzer,項目名稱:extreme,代碼行數:4,代碼來源:extremeValues.go

示例15: Shuffle

// Shuffle shuffles the cards in the deck using the
// Knuth shuffle algorithm.
func (d Deck) Shuffle(rng *rand.Rand) {
	for a := 0; a < len(d); a++ {
		b := rng.Int31n(int32(a) + 1)
		d[a], d[b] = d[b], d[a]
	}
}
開發者ID:jteeuwen,項目名稱:deck,代碼行數:8,代碼來源:deck.go


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