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


Golang Rand.Read方法代码示例

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


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

示例1: RandData

func RandData(r *rand.Rand, n int, useCached bool) []byte {
	if useCached && n <= predataLength {
		return predata[:n]
	}

	b := make([]byte, n)
	r.Read(b)

	ret := make([]byte, n)
	for i := range b {
		ret[i] = letters[b[i]]
	}

	return ret
}
开发者ID:Netflix,项目名称:rend,代码行数:15,代码来源:utils.go

示例2: RandDatum

// RandDatum generates a random Datum of the given type.
// If null is true, the datum can be DNull.
func RandDatum(rng *rand.Rand, typ ColumnType_Kind, null bool) parser.Datum {
	if null && rng.Intn(10) == 0 {
		return parser.DNull
	}
	switch typ {
	case ColumnType_BOOL:
		return parser.MakeDBool(rng.Intn(2) == 1)
	case ColumnType_INT:
		return parser.NewDInt(parser.DInt(rng.Int63()))
	case ColumnType_FLOAT:
		return parser.NewDFloat(parser.DFloat(rng.NormFloat64()))
	case ColumnType_DECIMAL:
		d := &parser.DDecimal{}
		d.Dec.SetScale(inf.Scale(rng.Intn(40) - 20))
		d.Dec.SetUnscaled(rng.Int63())
		return d
	case ColumnType_DATE:
		return parser.NewDDate(parser.DDate(rng.Intn(10000)))
	case ColumnType_TIMESTAMP:
		return &parser.DTimestamp{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))}
	case ColumnType_INTERVAL:
		return &parser.DInterval{Duration: duration.Duration{Months: rng.Int63n(1000),
			Days:  rng.Int63n(1000),
			Nanos: rng.Int63n(1000000),
		}}
	case ColumnType_STRING:
		// Generate a random ASCII string.
		p := make([]byte, rng.Intn(10))
		for i := range p {
			p[i] = byte(1 + rng.Intn(127))
		}
		return parser.NewDString(string(p))
	case ColumnType_BYTES:
		p := make([]byte, rng.Intn(10))
		_, _ = rng.Read(p)
		return parser.NewDBytes(parser.DBytes(p))
	case ColumnType_TIMESTAMPTZ:
		return &parser.DTimestampTZ{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))}
	case ColumnType_INT_ARRAY:
		// TODO(cuongdo): we don't support for persistence of arrays yet
		return parser.DNull
	default:
		panic(fmt.Sprintf("invalid type %s", typ))
	}
}
开发者ID:jmptrader,项目名称:cockroach,代码行数:47,代码来源:testutils.go

示例3: encryptRandom

// encryptRandom encrypts the input, with 5-10 random bytes added
// before and after it, under a random key. If mode is "ECB", ECB mode
// is used; otherwise CBC mode is used with a random initialization vector.
func encryptRandom(input []byte, mode string, r rand.Rand) []byte {
	// start with 5-10 random bytes, then append the input, then 5-10
	// more random bytes
	s := make([]byte, 5+r.Intn(6))
	r.Read(s)
	s = append(s, input...)
	end := make([]byte, 5+r.Intn(6))
	r.Read(end)
	s = append(s, end...)

	// random key
	key := make([]byte, 16)
	r.Read(key)

	if mode == "ECB" {
		return EncryptAes128Ecb(s, key)
	}

	// CBC mode with random IV
	iv := make([]byte, 16)
	r.Read(iv)
	return EncryptAes128Cbc(s, key, iv)
}
开发者ID:klothe,项目名称:matasano-crypto-challenge-solutions-go,代码行数:26,代码来源:set2.go

示例4: RandDatum

// RandDatum generates a random Datum of the given type.
// If null is true, the datum can be DNull.
func RandDatum(rng *rand.Rand, typ ColumnType, null bool) parser.Datum {
	if null && rng.Intn(10) == 0 {
		return parser.DNull
	}
	switch typ.Kind {
	case ColumnType_BOOL:
		return parser.MakeDBool(rng.Intn(2) == 1)
	case ColumnType_INT:
		return parser.NewDInt(parser.DInt(rng.Int63()))
	case ColumnType_FLOAT:
		return parser.NewDFloat(parser.DFloat(rng.NormFloat64()))
	case ColumnType_DECIMAL:
		d := &parser.DDecimal{}
		d.Dec.SetScale(inf.Scale(rng.Intn(40) - 20))
		d.Dec.SetUnscaled(rng.Int63())
		return d
	case ColumnType_DATE:
		return parser.NewDDate(parser.DDate(rng.Intn(10000)))
	case ColumnType_TIMESTAMP:
		return &parser.DTimestamp{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))}
	case ColumnType_INTERVAL:
		return &parser.DInterval{Duration: duration.Duration{Months: rng.Int63n(1000),
			Days:  rng.Int63n(1000),
			Nanos: rng.Int63n(1000000),
		}}
	case ColumnType_STRING:
		// Generate a random ASCII string.
		p := make([]byte, rng.Intn(10))
		for i := range p {
			p[i] = byte(1 + rng.Intn(127))
		}
		return parser.NewDString(string(p))
	case ColumnType_BYTES:
		p := make([]byte, rng.Intn(10))
		_, _ = rng.Read(p)
		return parser.NewDBytes(parser.DBytes(p))
	case ColumnType_TIMESTAMPTZ:
		return &parser.DTimestampTZ{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))}
	case ColumnType_COLLATEDSTRING:
		if typ.Locale == nil {
			panic("locale is required for COLLATEDSTRING")
		}
		// Generate a random Unicode string.
		var buf bytes.Buffer
		n := rng.Intn(10)
		for i := 0; i < n; i++ {
			var r rune
			for {
				r = rune(rng.Intn(unicode.MaxRune + 1))
				if !unicode.Is(unicode.C, r) {
					break
				}
			}
			buf.WriteRune(r)
		}
		return parser.NewDCollatedString(buf.String(), *typ.Locale, &parser.CollationEnvironment{})
	case ColumnType_INT_ARRAY:
		// TODO(cuongdo): we don't support for persistence of arrays yet
		return parser.DNull
	default:
		panic(fmt.Sprintf("invalid type %s", typ.String()))
	}
}
开发者ID:BramGruneir,项目名称:cockroach,代码行数:65,代码来源:testutils.go

示例5: randBuf

func randBuf(rng *rand.Rand, maxLen int) []byte {
	buf := make([]byte, rng.Intn(maxLen+1))
	_, _ = rng.Read(buf)
	return buf
}
开发者ID:knz,项目名称:cockroach,代码行数:5,代码来源:decimal_test.go


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