本文整理汇总了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)
}
示例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)
}
示例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
}
示例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
}
示例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)
}
}
示例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()
}
示例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)
}
示例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
}
示例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
}
}
}
示例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
}
示例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)
}
示例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
}
示例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)
}
示例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)
}
示例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)
}