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


Golang Rand.Intn方法代码示例

本文整理汇总了Golang中rand.Rand.Intn方法的典型用法代码示例。如果您正苦于以下问题:Golang Rand.Intn方法的具体用法?Golang Rand.Intn怎么用?Golang Rand.Intn使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在rand.Rand的用法示例。


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

示例1: MontePathIn

// MontePathIn computes montecarlo distribution and flow for pathing
// in to the set minimum depth, N samples per start location.
func (f *Fill) MontePathIn(r *rand.Rand, start []Location, N int, MinDepth uint16) (dist []int, flow [][4]int) {
	dist = make([]int, len(f.Depth))
	flow = make([][4]int, len(f.Depth))

	for _, origloc := range start {
		for n := 0; n < N; n++ {
			loc := origloc
			d := 0

			for d < 4 {
				depth := f.Depth[loc]
				nperm := r.Intn(24)
				for d = 0; d < 4; d++ {
					nloc := f.LocStep[loc][Perm4[nperm][d]]
					if f.Depth[nloc] < depth && f.Depth[nloc] > MinDepth {
						flow[loc][Perm4[nperm][d]]++
						loc = nloc
						dist[loc]++
						break
					}
				}
			}
		}
	}
	return
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:28,代码来源:pathing.go

示例2: Generate

func (*certificateMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &certificateMsg{}
	numCerts := rand.Intn(20)
	m.certificates = make([][]byte, numCerts)
	for i := 0; i < numCerts; i++ {
		m.certificates[i] = randomBytes(rand.Intn(10)+1, rand)
	}
	return reflect.ValueOf(m)
}
开发者ID:Quantumboost,项目名称:gcc,代码行数:9,代码来源:handshake_messages_test.go

示例3: Generate

func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientHelloMsg{}
	m.major = uint8(rand.Intn(256))
	m.minor = uint8(rand.Intn(256))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuites = make([]uint16, rand.Intn(63)+1)
	for i := 0; i < len(m.cipherSuites); i++ {
		m.cipherSuites[i] = uint16(rand.Int31())
	}
	m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)

	return reflect.NewValue(m)
}
开发者ID:8l,项目名称:go-learn,代码行数:14,代码来源:handshake_messages_test.go

示例4: Generate

func (b Bitmask) Generate(rand *rand.Rand, size int) reflect.Value {
	result := Bitmask{
		x: size - rand.Intn(size),
		y: size - rand.Intn(size),
		w: rand.Intn(size),
		h: rand.Intn(size),
	}
	result.lines = make([][]part, result.h)
	completeness := rand.Intn(size)
	for y := 0; y < result.h; y++ {
		result.lines[y] = make([]part, result.w/sz+1)
		for x := 0; x < result.w; x++ {
			result.SetRel(x, y, rand.Intn(completeness) == 0)
		}
	}

	return reflect.NewValue(result)
}
开发者ID:goj,项目名称:go-mask,代码行数:18,代码来源:bitmask_test.go

示例5: Generate

func (*clientHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	m := &clientHelloMsg{}
	m.vers = uint16(rand.Intn(65536))
	m.random = randomBytes(32, rand)
	m.sessionId = randomBytes(rand.Intn(32), rand)
	m.cipherSuites = make([]uint16, rand.Intn(63)+1)
	for i := 0; i < len(m.cipherSuites); i++ {
		m.cipherSuites[i] = uint16(rand.Int31())
	}
	m.compressionMethods = randomBytes(rand.Intn(63)+1, rand)
	if rand.Intn(10) > 5 {
		m.nextProtoNeg = true
	}
	if rand.Intn(10) > 5 {
		m.serverName = randomString(rand.Intn(255), rand)
	}
	m.ocspStapling = rand.Intn(10) > 5

	return reflect.NewValue(m)
}
开发者ID:GNA-SERVICES-INC,项目名称:MoNGate,代码行数:20,代码来源:handshake_messages_test.go

示例6: Permute5

// Draw from the arrays with rand.Intn(120) for a random permutation of directions including NoMovement
func Permute5(r *rand.Rand) *[5]Direction {
	return &Perm5[r.Intn(120)]
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:4,代码来源:util.go

示例7: Permute5D

// Return a set of directions with d first, opposite last an the other 3 permuted.
func Permute5D(d Direction, r *rand.Rand) *[5]Direction {
	return &PermStepD5[d][r.Intn(6)]
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:4,代码来源:util.go

示例8: Permute4

// Draw from the arrays with rand.Intn(24) for a random permutation of directions.
func Permute4(r *rand.Rand) *[4]Direction {
	return &Perm4[r.Intn(24)]
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:4,代码来源:util.go

示例9: Permute4G

// Returns a permute4 + guard used in eg NPathIn
func Permute4G(r *rand.Rand) *[5]Direction {
	return &Perm4G[r.Intn(24)]
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:4,代码来源:util.go

示例10: Value

// Value returns an arbitrary value of the given type.
// If the type implements the Generator interface, that will be used.
// Note: in order to create arbitrary values for structs, all the members must be public.
func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
	if m, ok := reflect.MakeZero(t).Interface().(Generator); ok {
		return m.Generate(rand, complexSize), true
	}

	switch concrete := t.(type) {
	case *reflect.BoolType:
		return reflect.NewValue(rand.Int()&1 == 0), true
	case *reflect.Float32Type:
		return reflect.NewValue(randFloat32(rand)), true
	case *reflect.Float64Type:
		return reflect.NewValue(randFloat64(rand)), true
	case *reflect.FloatType:
		if t.Size() == 4 {
			return reflect.NewValue(float(randFloat32(rand))), true
		} else {
			return reflect.NewValue(float(randFloat64(rand))), true
		}
	case *reflect.Int16Type:
		return reflect.NewValue(int16(randInt64(rand))), true
	case *reflect.Int32Type:
		return reflect.NewValue(int32(randInt64(rand))), true
	case *reflect.Int64Type:
		return reflect.NewValue(randInt64(rand)), true
	case *reflect.Int8Type:
		return reflect.NewValue(int8(randInt64(rand))), true
	case *reflect.IntType:
		return reflect.NewValue(int(randInt64(rand))), true
	case *reflect.MapType:
		numElems := rand.Intn(complexSize)
		m := reflect.MakeMap(concrete)
		for i := 0; i < numElems; i++ {
			key, ok1 := Value(concrete.Key(), rand)
			value, ok2 := Value(concrete.Elem(), rand)
			if !ok1 || !ok2 {
				return nil, false
			}
			m.SetElem(key, value)
		}
		return m, true
	case *reflect.PtrType:
		v, ok := Value(concrete.Elem(), rand)
		if !ok {
			return nil, false
		}
		p := reflect.MakeZero(concrete)
		p.(*reflect.PtrValue).PointTo(v)
		return p, true
	case *reflect.SliceType:
		numElems := rand.Intn(complexSize)
		s := reflect.MakeSlice(concrete, numElems, numElems)
		for i := 0; i < numElems; i++ {
			v, ok := Value(concrete.Elem(), rand)
			if !ok {
				return nil, false
			}
			s.Elem(i).SetValue(v)
		}
		return s, true
	case *reflect.StringType:
		numChars := rand.Intn(complexSize)
		codePoints := make([]int, numChars)
		for i := 0; i < numChars; i++ {
			codePoints[i] = rand.Intn(0x10ffff)
		}
		return reflect.NewValue(string(codePoints)), true
	case *reflect.StructType:
		s := reflect.MakeZero(t).(*reflect.StructValue)
		for i := 0; i < s.NumField(); i++ {
			v, ok := Value(concrete.Field(i).Type, rand)
			if !ok {
				return nil, false
			}
			s.Field(i).SetValue(v)
		}
		return s, true
	case *reflect.Uint16Type:
		return reflect.NewValue(uint16(randInt64(rand))), true
	case *reflect.Uint32Type:
		return reflect.NewValue(uint32(randInt64(rand))), true
	case *reflect.Uint64Type:
		return reflect.NewValue(uint64(randInt64(rand))), true
	case *reflect.Uint8Type:
		return reflect.NewValue(uint8(randInt64(rand))), true
	case *reflect.UintType:
		return reflect.NewValue(uint(randInt64(rand))), true
	case *reflect.UintptrType:
		return reflect.NewValue(uintptr(randInt64(rand))), true
	default:
		return nil, false
	}

	return
}
开发者ID:lougxing,项目名称:golang-china,代码行数:97,代码来源:quick.go

示例11: Value

// Value returns an arbitrary value of the given type.
// If the type implements the Generator interface, that will be used.
// Note: in order to create arbitrary values for structs, all the members must be public.
func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
	if m, ok := reflect.Zero(t).Interface().(Generator); ok {
		return m.Generate(rand, complexSize), true
	}

	switch concrete := t; concrete.Kind() {
	case reflect.Bool:
		return reflect.ValueOf(rand.Int()&1 == 0), true
	case reflect.Float32:
		return reflect.ValueOf(randFloat32(rand)), true
	case reflect.Float64:
		return reflect.ValueOf(randFloat64(rand)), true
	case reflect.Complex64:
		return reflect.ValueOf(complex(randFloat32(rand), randFloat32(rand))), true
	case reflect.Complex128:
		return reflect.ValueOf(complex(randFloat64(rand), randFloat64(rand))), true
	case reflect.Int16:
		return reflect.ValueOf(int16(randInt64(rand))), true
	case reflect.Int32:
		return reflect.ValueOf(int32(randInt64(rand))), true
	case reflect.Int64:
		return reflect.ValueOf(randInt64(rand)), true
	case reflect.Int8:
		return reflect.ValueOf(int8(randInt64(rand))), true
	case reflect.Int:
		return reflect.ValueOf(int(randInt64(rand))), true
	case reflect.Uint16:
		return reflect.ValueOf(uint16(randInt64(rand))), true
	case reflect.Uint32:
		return reflect.ValueOf(uint32(randInt64(rand))), true
	case reflect.Uint64:
		return reflect.ValueOf(uint64(randInt64(rand))), true
	case reflect.Uint8:
		return reflect.ValueOf(uint8(randInt64(rand))), true
	case reflect.Uint:
		return reflect.ValueOf(uint(randInt64(rand))), true
	case reflect.Uintptr:
		return reflect.ValueOf(uintptr(randInt64(rand))), true
	case reflect.Map:
		numElems := rand.Intn(complexSize)
		m := reflect.MakeMap(concrete)
		for i := 0; i < numElems; i++ {
			key, ok1 := Value(concrete.Key(), rand)
			value, ok2 := Value(concrete.Elem(), rand)
			if !ok1 || !ok2 {
				return reflect.Value{}, false
			}
			m.SetMapIndex(key, value)
		}
		return m, true
	case reflect.Ptr:
		v, ok := Value(concrete.Elem(), rand)
		if !ok {
			return reflect.Value{}, false
		}
		p := reflect.New(concrete.Elem())
		p.Elem().Set(v)
		return p, true
	case reflect.Slice:
		numElems := rand.Intn(complexSize)
		s := reflect.MakeSlice(concrete, numElems, numElems)
		for i := 0; i < numElems; i++ {
			v, ok := Value(concrete.Elem(), rand)
			if !ok {
				return reflect.Value{}, false
			}
			s.Index(i).Set(v)
		}
		return s, true
	case reflect.String:
		numChars := rand.Intn(complexSize)
		codePoints := make([]int, numChars)
		for i := 0; i < numChars; i++ {
			codePoints[i] = rand.Intn(0x10ffff)
		}
		return reflect.ValueOf(string(codePoints)), true
	case reflect.Struct:
		s := reflect.New(t).Elem()
		for i := 0; i < s.NumField(); i++ {
			v, ok := Value(concrete.Field(i).Type, rand)
			if !ok {
				return reflect.Value{}, false
			}
			s.Field(i).Set(v)
		}
		return s, true
	default:
		return reflect.Value{}, false
	}

	return
}
开发者ID:go-nosql,项目名称:golang,代码行数:95,代码来源:quick.go


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