本文整理汇总了Golang中math/rand.Rand.Int31方法的典型用法代码示例。如果您正苦于以下问题:Golang Rand.Int31方法的具体用法?Golang Rand.Int31怎么用?Golang Rand.Int31使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/rand.Rand
的用法示例。
在下文中一共展示了Rand.Int31方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
db, err := sql.Open("postgres", "user=postgres host=localhost port=5432 dbname=example_db sslmode=disable")
if err != nil {
log.Fatalln(err)
}
defer func() {
fmt.Println("close.")
if err := db.Close(); err != nil {
log.Fatalln(err)
}
}()
// resetDB(db)
if tx, err := db.Begin(); err != nil {
log.Fatalln("begin ", err)
} else {
var r *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
if res, err := tx.Exec(INSERT_TEST_TBL, r.Int31(), "hoge", "fugafuga"); err != nil {
log.Fatalln("insert query ", err)
} else {
fmt.Printf("%+v\n", res)
}
defer func() {
fmt.Println("commit.")
if err := tx.Commit(); err != nil {
log.Fatalln(err)
}
}()
}
}
示例2: GenerateDomain
func GenerateDomain(r *rand.Rand, size int) []byte {
dnLen := size % 70 // artificially limit size so there's less to intrepret if a failure occurs
var dn []byte
done := false
for i := 0; i < dnLen && !done; {
max := dnLen - i
if max > 63 {
max = 63
}
lLen := max
if lLen != 0 {
lLen = int(r.Int31()) % max
}
done = lLen == 0
if done {
continue
}
l := make([]byte, lLen+1)
l[0] = byte(lLen)
for j := 0; j < lLen; j++ {
l[j+1] = byte(rand.Int31())
}
dn = append(dn, l...)
i += 1 + lLen
}
return append(dn, 0)
}
示例3: Gen
// Generates a random number according to the distribution using the rng passed.
func (al *Alias) Gen(rng *rand.Rand) uint32 {
ri := uint32(rng.Int31())
w := ri % uint32(len(al.table))
if ri > al.table[w].prob {
return al.table[w].alias
}
return w
}
示例4: RandomSecret
// Generate a Random secret encoded as a b32 string
// If the length is <= 0, a default length of 10 bytes will
// be used, which will generate a secret of length 16.
func RandomSecret(length int, rnd *rand.Rand) string {
if 0 <= length {
length = 10
}
secret := make([]byte, length)
for i, _ := range secret {
secret[i] = byte(rnd.Int31() % 256)
}
return base32.StdEncoding.EncodeToString(secret)
}
示例5: 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
}
示例6: NonZeroRand32
func NonZeroRand32(rnd *rand.Rand) int32 {
for {
r := rnd.Int31()
if r == 0 {
continue
}
if rnd.Intn(1) != 0 {
return -r
}
return r
}
}
示例7: 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
}
示例8: Generate
func (*serverHelloMsg) Generate(rand *rand.Rand, size int) reflect.Value {
m := &serverHelloMsg{}
m.vers = uint16(rand.Intn(65536))
m.random = randomBytes(32, rand)
m.sessionId = randomBytes(rand.Intn(32), rand)
m.cipherSuite = uint16(rand.Int31())
m.compressionMethod = uint8(rand.Intn(256))
if rand.Intn(10) > 5 {
m.nextProtoNeg = true
n := rand.Intn(10)
m.nextProtos = make([]string, n)
for i := 0; i < n; i++ {
m.nextProtos[i] = randomString(20, rand)
}
}
if rand.Intn(10) > 5 {
m.ocspStapling = true
}
if rand.Intn(10) > 5 {
m.ticketSupported = true
}
return reflect.ValueOf(m)
}
示例9: GenerateTXT
func GenerateTXT(r *rand.Rand, size int) []byte {
rdLen := size % 300 // artificially limit size so there's less to intrepret if a failure occurs
var rd []byte
for i := 0; i < rdLen; {
max := rdLen - 1
if max > 255 {
max = 255
}
sLen := max
if max != 0 {
sLen = int(r.Int31()) % max
}
s := make([]byte, sLen+1)
s[0] = byte(sLen)
for j := 0; j < sLen; j++ {
s[j+1] = byte(rand.Int31())
}
rd = append(rd, s...)
i += 1 + sLen
}
return rd
}
示例10: 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)
}
示例11: timeGenerator
func timeGenerator(rand *rand.Rand) time.Time {
months := []time.Month{
time.January, time.February, time.March,
time.April, time.May, time.June,
time.July, time.August, time.September,
time.October, time.November, time.December,
}
return time.Date(
rand.Intn(9999),
months[rand.Intn(len(months))],
rand.Intn(31),
rand.Intn(24),
rand.Intn(60),
rand.Intn(60),
int(rand.Int31()),
time.UTC,
)
}
示例12: randomBytes
func randomBytes(out []byte, rand *rand.Rand) {
for i := 0; i < len(out); i++ {
out[i] = byte(rand.Int31())
}
}
示例13: NewPopulatedPreAccept
func NewPopulatedPreAccept(r *math_rand.Rand) *PreAccept {
this := &PreAccept{}
this.LeaderId = r.Int31()
this.Replica = r.Int31()
this.Instance = r.Int31()
this.Ballot = r.Int31()
v1 := r.Intn(100)
this.Command = make([]byte, v1)
for i := 0; i < v1; i++ {
this.Command[i] = byte(r.Intn(256))
}
this.Seq = r.Int31()
for i := 0; i < 5; i++ {
this.Deps[i] = r.Int31()
}
return this
}
示例14: RandomGenerate
func RandomGenerate(r *rand.Rand, b []byte) {
for i, _ := range b {
// the common value in [0x0f, 0xf0]
b[i] = 0x0f + byte(r.Int31()%(256-0x0f-0x0f))
}
}
示例15: getStructureOfOneFile
//.........这里部分代码省略.........
// Store id's of references
if s.Is("ul.references") { // references detected
s.Find("li").Each(func(i int, s2 *goquery.Selection) {
id, exists := s2.Attr("id")
if !exists || id == "" || id == "#" {
// No id is present, ignore this list item
return
} else {
// Find text between <strong> ... </strong>
tooltip := ""
s2.Find("strong").Each(func(i int, s3 *goquery.Selection) {
tooltip = s3.Text()
})
// Store id as bookmark
title, exists := s2.Attr("title")
if exists && title != "" {
addBookmark(id, fileName, title, tooltip)
} else {
addBookmark(id, fileName, "", tooltip)
}
}
})
return
}
// Inquire element id and content (= text + label)
var label string
newID := false
id, exists := s.Attr("id")
if !exists || id == "" || id == "#" {
// If no id present, introduce a random value for id
id = strconv.Itoa(int(r.Int31()))
newID = true
}
// text := s.Text()
text, _ := s.Html()
modified := false // = true, if text is modified
var newText string
// Actual index of SectionFiles
iFile := len(BookStructure.SectionFiles) - 1
// Store information
if s.Is("h1") {
Counters.iFigCaption = 0
Counters.iCaption = 0
Counters.iEquation = 0
// Determine chapter number
isec := minInt(len("Chapter"), len(text))
if text[0:isec] == "Chapter" {
// Increment chapter number
Counters.ih1_digit++
Counters.last_h1_type = "Chapter"
} else {
isec = minInt(len("Appendix"), len(text))
if text[0:isec] == "Appendix" {
// Increment appendix number
Counters.ih1_letter++
Counters.last_h1_type = "Appendix"
} else {
Counters.last_h1_type = ""
}
}