当前位置: 首页>>代码示例>>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;未经允许,请勿转载。