當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。