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


Golang rand.Int函数代码示例

本文整理汇总了Golang中rand.Int函数的典型用法代码示例。如果您正苦于以下问题:Golang Int函数的具体用法?Golang Int怎么用?Golang Int使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: Crossover

func (g *GeneticElement) Crossover(m *GeneticElement, f *GeneticElement) {
	var mMask uint64
	var fMask uint64
	if m.initialized && f.initialized {
		guess := rand.Int() % 10
		if guess < 6 { //euristic value for ricombination
			cxIndex := uint(rand.Int() % len(f.tRef))
			mMask = 0xFFFFFFFFFFFFFFFF << cxIndex
			fMask = 0xFFFFFFFFFFFFFFFF ^ mMask
			g.chromosome = (fMask & f.chromosome) | (mMask & m.chromosome)
		} else {
			g.chromosome = f.chromosome
		}
		guess = rand.Int() % 100 // generate a mutation into genoma
		if guess <= 1 {
			mutatedbit := uint(rand.Int() % len(f.tRef))
			mMask = 0x1 << mutatedbit
			g.chromosome = g.chromosome ^ mMask
		}

		g.tRef = f.tRef
		g.target = f.target
		g.value, g.weight = evalutate(g.chromosome, g.tRef)
		g.fitness = generate_fitness(g.weight, g.target)
		g.initialized = true
	}
}
开发者ID:spaghetty,项目名称:spagh_lib,代码行数:27,代码来源:knapsack.go

示例2: Create

func (nv StrValue) Create() string {
	if nv.size == 128 {
		substrs := [...]string{"ning", "izzy", "ariel", "nick",
			"mazur", "ryan", "hugg", "yankeesfan", "volt", "runs",
			"with", "scissors", "blue", "awesome", "weak", "sauce",
			"chicken", "strength", "vikram", "bobbi", "jarr", "bungee",
			"banjo", "arrow", "trinity", "coffee", "pvc"}
		cnt := (rand.Int() % 10) + 1
		vals := make([]string, cnt)

		for i := 0; i < cnt; i++ {
			vals[i] = substrs[rand.Int()%len(substrs)]
		}
		return strings.Join(vals, "")
	} else if nv.size == 4 {
		substrs := [...]string{"a", "b", "c", "d"}
		cnt := 4
		vals := make([]string, cnt)

		for i := 0; i < cnt; i++ {
			vals[i] = substrs[rand.Int()%len(substrs)]
		}
		return strings.Join(vals, "")
	}
	panic("Invalid string size.")
}
开发者ID:huanchenz,项目名称:h-store-index,代码行数:26,代码来源:index_script_gen.go

示例3: Crossover

func (g *GeneticElement) Crossover(m *GeneticElement, f *GeneticElement) {
	var mMask uint64
	var fMask uint64
	if m.initialized && f.initialized {
		guess := rand.Int() % 10
		if guess < 6 { //euristic value for ricombination
			cxIndex := uint(rand.Int() % 36)
			mMask = 0xFFFFFFFFFFFFFFFF << cxIndex
			fMask = 0xFFFFFFFFFFFFFFFF ^ mMask
			g.chromosome = (fMask & f.chromosome) | (mMask & m.chromosome)
		} else {
			g.chromosome = f.chromosome
		}
		guess = rand.Int() % 100 // generate a mutation into genoma
		if guess <= 1 {
			mutatedbit := uint(rand.Int() % 36)
			mMask = 0x1 << mutatedbit
			g.chromosome = g.chromosome ^ mMask
		}

		g.maptable = f.maptable
		g.computeFitness()
		g.initialized = true
	}
}
开发者ID:spaghetty,项目名称:spagh_lib,代码行数:25,代码来源:genetic.go

示例4: main

func main() {
	flag.Parse()
	//	prime();
	var blocks [1]struct {
		base *byte
		siz  uintptr
	}
	for i := 0; i < 1<<10; i++ {
		if i%(1<<10) == 0 && *chatty {
			println(i)
		}
		b := rand.Int() % len(blocks)
		if blocks[b].base != nil {
			//	println("Free", blocks[b].siz, blocks[b].base);
			runtime.Free(blocks[b].base)
			blocks[b].base = nil
			allocated -= uint64(blocks[b].siz)
			continue
		}
		siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
		base := runtime.Alloc(siz)
		//	ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2);
		//	obj, size, ref, ok := allocator.find(ptr);
		//	if obj != base || *ref != 0 || !ok {
		//		panicln("find", siz, obj, ref, ok);
		//	}
		blocks[b].base = base
		blocks[b].siz = siz
		allocated += uint64(siz)
		//	println("Alloc", siz, base);
		memset(base, 0xbb, siz)
		bigger()
	}
}
开发者ID:ivanwyc,项目名称:google-go,代码行数:34,代码来源:mallocrand.go

示例5: randInts

func randInts() []int {
	r := rand.Int() % 30
	d := make([]int, r)
	for i := 0; i < r; i++ {
		d[i] = rand.Int() % 30
	}
	return d
}
开发者ID:shxiao,项目名称:algo,代码行数:8,代码来源:main.go

示例6: createSchema

func createSchema() ([]NValue, []string) {
	schema := make([]NValue, (rand.Int()%maxIdxColumns)+1)
	abbrev := make([]string, len(schema))
	for ii := 0; ii < len(schema); ii++ {
		schemaType := voltIdxTypes[rand.Int()%len(voltIdxTypes)]
		schema[ii] = nvalueFactory(schemaType)
		abbrev[ii] = schemaType
	}
	return schema, abbrev
}
开发者ID:huanchenz,项目名称:h-store-index,代码行数:10,代码来源:index_script_gen.go

示例7: addHorde

func addHorde() {
	src := rand.NewSource(time.Nanoseconds())
	rng := rand.New(src)
	horde := make([]*core.User, 20000)

	// Add the horde.
	for i, _ := range horde {
		data := make([]core.DataChange, 4)
		data[0].Name, data[0].Data = "ip", fmt.Sprintf("%d.%d.%d.%d", rng.Int()%255, rng.Int()%255, rng.Int()%255, rng.Int()%255)
		data[1].Name, data[1].Data = "hostname", fmt.Sprintf("%d.Horde.FakeUsers.PsuedoUserUnion.org", rng.Int()%1000000)
		data[2].Name, data[2].Data = "ident", fmt.Sprintf("horde-%d", rng.Int()%1000000)
		data[3].Name, data[3].Data = "account", fmt.Sprintf("horde-%d", rng.Int()%1000000)

		horde[i] = core.NewUser("oddcomm/modules/dev/horde", nil, true, "", data)
		horde[i].SetNick(me, fmt.Sprintf("horde-%d", rng.Int()%1000000), -1)
		horde[i].PermitRegistration(me)
	}

	// Make a huge channel containing the entire horde.
	core.GetChannel("", "huge").Join(me, horde)

	// Make 100 channels containing roughly a twentieth of the horde each.
	// Each horde user is in an average of roughly five.
	for i := 0; i < 100; i++ {
		joiners := make([]*core.User, len(horde)/20)
		for i, _ := range joiners {
			joiners[i] = horde[rand.Int()%len(horde)]
		}
		name := fmt.Sprintf("big_%d", i)
		core.GetChannel("", name).Join(me, joiners)
	}

	// Make 2000 channels containing roughly 1/400th of the horde each.
	// Each horde user is in an average of roughly five.
	for i := 0; i < 2000; i++ {
		joiners := make([]*core.User, len(horde)/400)
		for i, _ := range joiners {
			joiners[i] = horde[rand.Int()%len(horde)]
		}
		name := fmt.Sprintf("medium_%d", i)
		core.GetChannel("", name).Join(me, joiners)
	}

	// Make horde*2 channels containing roughly four of the horde each.
	// Each horde user is in an average of roughly eight.
	for i := 0; i < len(horde)*2; i++ {
		joiners := make([]*core.User, 4)
		for i, _ := range joiners {
			joiners[i] = horde[rand.Int()%len(horde)]
		}
		name := fmt.Sprintf("small_%d", i)
		core.GetChannel("", name).Join(me, joiners)
	}
}
开发者ID:jbeshir,项目名称:OddComm,代码行数:54,代码来源:main.go

示例8: InitRand

func (p *GeneticElement) InitRand(e []Element, target int) {
	bitNum := rand.Int() % len(e)
	p.chromosome = uint64(0)
	for i := 0; i <= bitNum; i++ {
		tmp := uint64(1) << uint(rand.Int()%len(e))
		p.chromosome = p.chromosome | tmp
	}
	p.tRef = e[0:]
	p.target = target
	p.value, p.weight = evalutate(p.chromosome, e[0:])
	p.fitness = generate_fitness(p.weight, target)
	p.initialized = true
}
开发者ID:spaghetty,项目名称:spagh_lib,代码行数:13,代码来源:knapsack.go

示例9: TestKeyValue

func TestKeyValue(t *testing.T) {
	look_up_table := Make(true)
	for i := 0; i < 2000; i++ {
		look_up_table.Insert(key_value{rand.Int(), rand.Int()})
	}
	for kvi := range look_up_table.Iter(IN_ORDER) {
		kv := kvi.(key_value)
		val, found := look_up_table.Find(key_value{kv.key, 0})
		if !found || kv.value != val.(key_value).value {
			t.Errorf("Bad value: %v : %v != %v\n", kv, kv.value, val.(key_value).value)
		}
	}
}
开发者ID:Mukaram,项目名称:mudlark-go-pkgs,代码行数:13,代码来源:ll_rb_tree_test.go

示例10: sign

func sign() int {
	sign := rand.Int() % 2
	if sign == 0 {
		return 1
	}
	return -1
}
开发者ID:huanchenz,项目名称:h-store-index,代码行数:7,代码来源:index_script_gen.go

示例11: insert1000Records

func insert1000Records(t *testing.T, db *Client) map[uint64][]string {
	stmt, err := db.InitStmt()
	if err != nil {
		t.Fatalf("InitStmt: %v", err)
	}

	err = stmt.Prepare(INSERT_SIMPLE_STMT)
	if err != nil {
		t.Logf("Prepare insert: %v", err)
	}

	t.Logf("Insert 1000 records")
	rowMap := make(map[uint64][]string)
	for i := 0; i < 1000; i++ {
		num, str1, str2 := rand.Int(), randString(32), randString(128)
		err = stmt.BindParams(num, str1, str2)
		if err != nil {
			t.Fatalf("Error %s", err)
		}
		err = stmt.Execute()
		if err != nil {
			t.Fatalf("Error %s", err)
		}
		row := []string{fmt.Sprintf("%d", num), str1, str2}
		rowMap[stmt.LastInsertId] = row
	}
	return rowMap
}
开发者ID:ipeet,项目名称:camlistore,代码行数:28,代码来源:mysql_test.go

示例12: GetRandomClientByProvides

// This is simple today - it returns the first listed service that matches the request
// Load balancing needs to be applied here somewhere.
func GetRandomClientByProvides(provides string) (*rpc.Client, os.Error) {
	var providesList = make([]*Service, 0)

	var newClient *rpc.Client
	var err os.Error

	for _, v := range NS.Services {
		if v != nil {
			if v.Provides == provides {
				providesList = append(providesList, v)
			}

		}
	}

	if len(providesList) > 0 {
		random := rand.Int() % len(providesList)
		s := providesList[random]

		portString := fmt.Sprintf("%s:%d", s.IPAddress, s.Port)
		newClient, err = rpc.DialHTTP("tcp", portString)
		if err != nil {
			LogError(WARN, fmt.Sprintf("Found %d Clients to service %s request.", len(providesList), provides))
			return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
		}

	} else {
		return nil, NewError(NO_CLIENT_PROVIDES_SERVICE, provides)
	}
	return newClient, nil
}
开发者ID:paulbellamy,项目名称:skynet,代码行数:33,代码来源:config.go

示例13: findRand

func findRand(t *testing.T, conn *db.Connection, ch chan *vector.Vector) {
	stmt, sErr := conn.Prepare(
		"SELECT * FROM t WHERE i != ? ORDER BY RAND()")
	if sErr != nil {
		error(t, sErr, "Couldn't prepare")
	}

	rs, cErr := conn.Execute(stmt, rand.Int())
	if cErr != nil {
		error(t, cErr, "Couldn't select")
	}

	vout := new(vector.Vector)
	for res := range rs.Iter() {
		if res.Error() != nil {
			error(t, res.Error(), "Couldn't fetch")
		}
		vout.Push(res.Data())
	}

	if vout.Len() != len(tableT) {
		t.Error("Invalid length")
	}

	stmt.Close()
	ch <- vout
}
开发者ID:eden,项目名称:mysqlgo,代码行数:27,代码来源:mysql_test.go

示例14: serveTestFiles

func serveTestFiles(t *testing.T) (l net.Listener, err os.Error) {
	foundValidPort := false
	var port_str string
	for !foundValidPort {
		port := (rand.Int() & 0x7FFF) + 0x08000
		port_str = strconv.Itoa(port)
		addr, err := net.ResolveTCPAddr("127.0.0.1:" + port_str)
		if err != nil {
			t.Error("Create TCP Address: ", err.String())
			return nil, err
		}
		l, err = net.ListenTCP("tcp4", addr)
		if err != nil {
			if err == os.EADDRINUSE || strings.LastIndex(err.String(), os.EADDRINUSE.String()) != -1 {
				continue
			}
			t.Error("Unable to listen on TCP port: ", err.String())
			return l, err
		}
		foundValidPort = true
	}
	OverrideRapleafHostPort("127.0.0.1", port_str)
	go http.Serve(l, http.HandlerFunc(ServeTestHTTP))
	return l, err
}
开发者ID:pomack,项目名称:rapleaf-bindings,代码行数:25,代码来源:rapleaf_test.go

示例15: DialUri

// DialUri connects to one of the doozer servers given in `uri`. If `uri`
// contains a secret key, then DialUri will call `Access` with the secret.
func DialUri(uri string) (*Conn, os.Error) {
	if !strings.HasPrefix(uri, uriPrefix) {
		return nil, ErrInvalidUri
	}

	q := uri[len(uriPrefix):]
	p, err := http.ParseQuery(q)
	if err != nil {
		return nil, err
	}

	addrs, ok := p["ca"]
	if !ok {
		return nil, ErrInvalidUri
	}

	c, err := Dial(addrs[rand.Int()%len(addrs)])
	if err != nil {
		return nil, err
	}

	secret, ok := p["sk"]
	if ok {
		err = c.Access(secret[0])
		if err != nil {
			c.Close()
			return nil, err
		}
	}

	return c, nil
}
开发者ID:mloughran,项目名称:doozer,代码行数:34,代码来源:conn.go


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