本文整理汇总了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
}
示例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
}
示例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)
}
示例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)
}
}
}
}
}
示例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)
}
示例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]
}
}
示例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(),
}
}
示例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--
}
}
示例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()
}
示例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 ""
}
示例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)))
}
示例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)
}
示例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}
}
示例14: rndValueType
// rndValueType returns random value of type xValueType
func rndValueType(rnd *rand.Rand) xValueType {
return xValueType(rnd.Int31n(int32(xRnd + 1)))
}
示例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]
}
}