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


Golang Rand.Int31方法代码示例

本文整理汇总了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)
			}
		}()
	}
}
开发者ID:kyokomi-sandbox,项目名称:sandbox,代码行数:34,代码来源:sql.go

示例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)
}
开发者ID:abh,项目名称:dns,代码行数:27,代码来源:parse_test.go

示例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
}
开发者ID:encryptio,项目名称:alias,代码行数:9,代码来源:alias.go

示例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)
}
开发者ID:jasmeetc,项目名称:gototp,代码行数:13,代码来源:gototp.go

示例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
}
开发者ID:Jyggafey,项目名称:drone,代码行数:11,代码来源:messages_test.go

示例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
	}
}
开发者ID:gauravrmazra,项目名称:incubator-htrace,代码行数:12,代码来源:random.go

示例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
}
开发者ID:h8liu,项目名称:golang,代码行数:7,代码来源:handshake_messages_test.go

示例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)
}
开发者ID:h8liu,项目名称:golang,代码行数:27,代码来源:handshake_messages_test.go

示例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
}
开发者ID:abh,项目名称:dns,代码行数:22,代码来源:parse_test.go

示例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)
}
开发者ID:Jyggafey,项目名称:drone,代码行数:18,代码来源:messages_test.go

示例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,
	)
}
开发者ID:yanghongkjxy,项目名称:mtail,代码行数:19,代码来源:metric_test.go

示例12: randomBytes

func randomBytes(out []byte, rand *rand.Rand) {
	for i := 0; i < len(out); i++ {
		out[i] = byte(rand.Int31())
	}
}
开发者ID:Jyggafey,项目名称:drone,代码行数:5,代码来源:messages_test.go

示例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
}
开发者ID:hongchaodeng,项目名称:serialization-bench,代码行数:17,代码来源:gob_test.go

示例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))
	}
}
开发者ID:jingdi,项目名称:go-srs,代码行数:6,代码来源:utility.go

示例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 = ""
				}
			}
开发者ID:MartinOtter,项目名称:makeWebBook,代码行数:67,代码来源:makeWebBook.go


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