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


Golang rand.Rand类代码示例

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


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

示例1: randFloat32

// randFloat32 generates a random float taking the full range of a float32.
func randFloat32(rand *rand.Rand) float32 {
	f := rand.Float64() * math.MaxFloat32
	if rand.Int()&1 == 1 {
		f = -f
	}
	return float32(f)
}
开发者ID:go-nosql,项目名称:golang,代码行数:8,代码来源:quick.go

示例2: randomN

// randomN creates a random integer in [0..limit), using the space in z if
// possible. n is the bit length of limit.
func randomN(z []Word, rand *rand.Rand, limit []Word, n int) []Word {
	bitLengthOfMSW := uint(n % _W)
	if bitLengthOfMSW == 0 {
		bitLengthOfMSW = _W
	}
	mask := Word((1 << bitLengthOfMSW) - 1)
	z = makeN(z, len(limit), false)

	for {
		for i := range z {
			switch _W {
			case 32:
				z[i] = Word(rand.Uint32())
			case 64:
				z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32
			}
		}

		z[len(limit)-1] &= mask

		if cmpNN(z, limit) < 0 {
			break
		}
	}

	return normN(z)
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:29,代码来源:nat.go

示例3: 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

示例4: randFloat64

// randFloat64 generates a random float taking the full range of a float64.
func randFloat64(rand *rand.Rand) float64 {
	f := rand.Float64()
	if rand.Int()&1 == 1 {
		f = -f
	}
	return f
}
开发者ID:go-nosql,项目名称:golang,代码行数:8,代码来源:quick.go

示例5: outputDot

func (p *Trie) outputDot(vec *vector.StringVector, rune int, serial int64, rgen *rand.Rand) {
	this := make([]byte, 10)
	child := make([]byte, 10)

	utf8.EncodeRune(this, rune)

	thisChar := string(this[0])

	if serial == -1 {
		thisChar = "root"
	}

	for childRune, childNode := range p.children {
		utf8.EncodeRune(child, childRune)
		childSerial := rgen.Int63()
		childNodeStr := fmt.Sprintf("\"%s(%d)\"", string(child[0]), childSerial)
		var notation string

		if string(child[0]) == "/" {
			notation = fmt.Sprintf("[label=\"%s\" shape=box color=red]", string(child[0]))
		} else {
			notation = fmt.Sprintf("[label=\"%s\"]", string(child[0]))
		}
		vec.Push(fmt.Sprintf("\t%s %s\n\t\"%s(%d)\" -> \"%s(%d)\"", childNodeStr, notation, thisChar, serial, string(child[0]), childSerial))
		childNode.outputDot(vec, childRune, childSerial, rgen)
	}
}
开发者ID:alangenfeld,项目名称:cs639,代码行数:27,代码来源:trie.go

示例6: random

// random creates a random integer in [0..limit), using the space in z if
// possible. n is the bit length of limit.
func (z nat) random(rand *rand.Rand, limit nat, n int) nat {
	bitLengthOfMSW := uint(n % _W)
	if bitLengthOfMSW == 0 {
		bitLengthOfMSW = _W
	}
	mask := Word((1 << bitLengthOfMSW) - 1)
	z = z.make(len(limit))

	for {
		for i := range z {
			switch _W {
			case 32:
				z[i] = Word(rand.Uint32())
			case 64:
				z[i] = Word(rand.Uint32()) | Word(rand.Uint32())<<32
			}
		}

		z[len(limit)-1] &= mask

		if z.cmp(limit) < 0 {
			break
		}
	}

	return z.norm()
}
开发者ID:radhermit,项目名称:gcc,代码行数:29,代码来源:nat.go

示例7: 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

示例8: randomNameList

func randomNameList(rand *rand.Rand) []string {
	ret := make([]string, rand.Int31()&15)
	for i := range ret {
		s := make([]byte, 1+(rand.Int31()&15))
		for j := range s {
			s[j] = 'a' + uint8(rand.Int31()&15)
		}
		ret[i] = string(s)
	}
	return ret
}
开发者ID:Sunmonds,项目名称:gcc,代码行数:11,代码来源:messages_test.go

示例9: choose

func choose(fork forkChoice, random *rand.Rand) {
	pivot := random.Float64() * fork.TotalMass()
	fork.Reset()
	for prob, _, ok := fork.Next(); ok; prob, _, ok = fork.Next() {
		pivot -= prob
		if pivot <= 0.0 {
			fork.Pick()
			return
		}
	}
}
开发者ID:fedgrant,项目名称:tonika,代码行数:11,代码来源:fork.go

示例10: Sample

// Sample returns N random points sampled from a fill with step
// distance between low and hi inclusive.  it will return a count > 1
// if the sample size is smaller than N.  If n < 1 then return all
// points.
func (f *Fill) Sample(r *rand.Rand, n, low, high int) ([]Location, []int) {
	pool := make([]Location, 0, 200)
	lo, hi := uint16(low), uint16(high)
	for i, depth := range f.Depth {
		if depth >= lo && depth <= hi {
			pool = append(pool, Location(i))
		}
	}
	if n < 1 {
		return pool, nil
	}

	if len(pool) == 0 {
		return nil, nil
	}

	over := n / len(pool)
	perm := r.Perm(len(pool))[0 : n%len(pool)]
	if Debug[DBG_Sample] {
		log.Printf("Sample: Looking for %d explore points %d-%d, have %d possible", n, low, hi, len(pool))
	}

	var count []int
	if over > 0 {
		count = make([]int, len(pool))
		for i := range count {
			count[i] = over
		}
	} else {
		count = make([]int, len(perm))
	}

	for i := range perm {
		count[i]++
	}

	if over > 0 {
		return pool, count
	} else {
		pout := make([]Location, len(perm))
		for i, pi := range perm {
			if Debug[DBG_Sample] {
				log.Printf("Sample: adding location %d to output pool", pool[pi])
			}
			pout[i] = pool[pi]
		}
		return pout, count
	}

	return nil, nil
}
开发者ID:jcdny,项目名称:bugnuts,代码行数:55,代码来源:pathing.go

示例11: 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

示例12: randomBytes

func randomBytes(n int, rand *rand.Rand) []byte {
	r := make([]byte, n)
	for i := 0; i < n; i++ {
		r[i] = byte(rand.Int31())
	}
	return r
}
开发者ID:Quantumboost,项目名称:gcc,代码行数:7,代码来源:handshake_messages_test.go

示例13: 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

示例14: 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

示例15: Generate

func (*kexInitMsg) Generate(rand *rand.Rand, size int) reflect.Value {
	ki := &kexInitMsg{}
	randomBytes(ki.Cookie[:], rand)
	ki.KexAlgos = randomNameList(rand)
	ki.ServerHostKeyAlgos = randomNameList(rand)
	ki.CiphersClientServer = randomNameList(rand)
	ki.CiphersServerClient = randomNameList(rand)
	ki.MACsClientServer = randomNameList(rand)
	ki.MACsServerClient = randomNameList(rand)
	ki.CompressionClientServer = randomNameList(rand)
	ki.CompressionServerClient = randomNameList(rand)
	ki.LanguagesClientServer = randomNameList(rand)
	ki.LanguagesServerClient = randomNameList(rand)
	if rand.Int31()&1 == 1 {
		ki.FirstKexFollows = true
	}
	return reflect.ValueOf(ki)
}
开发者ID:Sunmonds,项目名称:gcc,代码行数:18,代码来源:messages_test.go


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