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


Golang rand.Seed函数代码示例

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


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

示例1: main

func main() {
	rand.Seed(time.Nanoseconds())
	m := ga.NewGAGaussianMutator(0.4, 0)
	b := new(ga.GA2PointBreeder)
	s := ga.NewGATournamentSelector(0.2, 5)

	i := new(ga.GARandomInitializer)
	gao := ga.NewGA(i, s, m, b)
	gao.PMutate = 0.5 //Chance of mutation
	gao.PBreed = 0.2  //Chance of breeding

	fmt.Printf("%s\n", gao)
	genome := ga.NewFloatGenome(make([]float64, 20), rosenbrock, 1, -1)
	gao.Init(100, genome) //Total population
	stopFunc := func(bestGenome ga.GAGenome) bool {
		return bestGenome.Score() <= 1e-2
	}
	generations := ga.OptimizeBest(gao, stopFunc, 1000) // Run genetic algorithm for 20 generations.
	/**
	for {
		ga.OptimizeNgenerations(gao, 100) // Run genetic algorithm for 20 generations.
		best := gao.Best().(*ga.GAFloatGenome)
		fmt.Printf("%s = %f\n", best, best.Score())
	}
	*/
	fmt.Printf("%s = %f\n", gao.Best(), gao.Best().Score())
	fmt.Printf("Number of generations = %d\n", generations)
	fmt.Printf("Calls to score = %d\n", scores)
}
开发者ID:StepLg,项目名称:go-galib,代码行数:29,代码来源:floating.go

示例2: main

func main() {
	context, _ := zmq.NewContext()
	socket, _ := context.NewSocket(zmq.PUB)
	defer context.Close()
	defer socket.Close()
	socket.Bind("tcp://*:5556")
	socket.Bind("ipc://weather.ipc")

	// Seed the random number generator
	rand.Seed(time.Nanoseconds())

	// loop for a while aparently
	for {

		//  make values that will fool the boss
		zipcode := rand.Intn(100000)
		temperature := rand.Intn(215) - 80
		relhumidity := rand.Intn(50) + 10

		msg := fmt.Sprintf("%d %d %d", zipcode, temperature, relhumidity)

		//  Send message to all subscribers
		socket.Send([]byte(msg), 0)
	}
}
开发者ID:rjsmith,项目名称:zguide,代码行数:25,代码来源:wuserver.go

示例3: main

func main() {
	rand.Seed(time.Nanoseconds())

	param := ga.GAParameter{
		Initializer: new(ga.GARandomInitializer),
		Selector:    ga.NewGATournamentSelector(0.2, 5),
		Breeder:     new(ga.GA2PointBreeder),
		Mutator:     ga.NewGAGaussianMutator(0.4, 0),
		PMutate:     0.5,
		PBreed:      0.2}

	// Second parameter is the number of Optimize Processes.
	gao := ga.NewGAParallel(param, 4)

	genome := ga.NewFloatGenome(make([]float64, 20), rosenbrock, 1, -1)

	gao.Init(100, genome) //Total population

	gao.OptimizeUntil(func(best ga.GAGenome) bool {
		return best.Score() < 1e-3
	})

	best := gao.Best().(*ga.GAFloatGenome)
	fmt.Printf("%s = %f\n", best, best.Score())
	fmt.Printf("Calls to score = %d\n", scores)
}
开发者ID:newblue,项目名称:go-galib,代码行数:26,代码来源:floating_parallel.go

示例4: main

func main() {
	rand.Seed(time.Seconds())
	nums := rand.Perm(MAX)[:COUNT]
	for _, val := range nums {
		fmt.Println(val)
	}
}
开发者ID:dhconnelly,项目名称:abandoned,代码行数:7,代码来源:prob4.go

示例5: main

func main() {
	rand.Seed(time.Nanoseconds())

	m := ga.NewMultiMutator()
	msh := new(ga.GAShiftMutator)
	msw := new(ga.GASwitchMutator)
	m.Add(msh)
	m.Add(msw)

	param := ga.GAParameter{
		Initializer: new(ga.GARandomInitializer),
		Selector:    ga.NewGATournamentSelector(0.7, 5),
		Breeder:     new(ga.GA2PointBreeder),
		Mutator:     m,
		PMutate:     0.2,
		PBreed:      0.7}

	gao := ga.NewGA(param)

	genome := ga.NewOrderedIntGenome([]int{10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, score)

	gao.Init(200, genome) //Total population

	gao.Optimize(20) // Run genetic algorithm for 20 generations.
	gao.PrintTop(10)
	fmt.Printf("Calls to score = %d\n", scores)
	fmt.Printf("%s\n", m.Stats())
}
开发者ID:newblue,项目名称:go-galib,代码行数:28,代码来源:ordered_int.go

示例6: main

func main() {
	duplicate := 0
	unknown := 0

	rand.Seed(time.Nanoseconds())

	m := hashmap.New()

	for i := 0; i < N; i++ {
		r := rand.Intn(S)
		if m.Has(Integer(r)) {
			duplicate++
		} else {
			m.Insert(Integer(r), true)
		}
	}

	for i := 0; i < N; i++ {
		r := rand.Intn(S)
		if m.Has(Integer(r)) {
			m.Remove(Integer(r))
		} else {
			unknown++
		}
	}

	fmt.Printf("%d insertions, %d duplicates\n", N, duplicate)
	fmt.Printf("%d removals, %d unknown\n", N, unknown)
	fmt.Printf("%d left\n", m.Len())
}
开发者ID:phf,项目名称:go-hashmap,代码行数:30,代码来源:test_random.go

示例7: main

func main() {
	rand.Seed(time.Nanoseconds())
	//m := new(ga.GASwitchMutator);
	m := ga.NewMultiMutator()
	msh := new(ga.GAShiftMutator)
	msw := new(ga.GASwitchMutator)
	m.Add(msh)
	m.Add(msw)
	b := new(ga.GA2PointBreeder)

	s := new(ga.GATournamentSelector)
	s.Contestants = 5 //Contestants in Tournament
	s.PElite = 0.7    //Chance of best contestant winning, chance of next is PElite^2 and so on.

	i := new(ga.GARandomInitializer)
	gao := ga.NewGA(i, s, m, b)
	gao.PMutate = 0.2 //Chance of mutation
	gao.PBreed = 0.2  //Chance of breeding

	fmt.Printf("%s\n", gao)
	genome := ga.NewOrderedIntGenome([]int{10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0}, score)
	gao.Init(200, genome) //Total population

	ga.OptimizeNgenerations(gao, 20) // Run genetic algorithm for 20 generations.
	gao.PrintTop(10)
	fmt.Printf("Calls to score = %d\n", scores)
	fmt.Printf("%s\n", m.Stats())
}
开发者ID:StepLg,项目名称:go-galib,代码行数:28,代码来源:ordered_int.go

示例8: Generate

func Generate(m *Mchain, count int) string {
	rand.Seed(time.Nanoseconds())
	t := m.Beginnings[rand.Intn(len(m.Beginnings)-1)]
	first, second := t.First, t.Second

	ret := make([]string, count, count)
	ret[0] = t.First
	ret[1] = t.Second

	var word string
	for i := 2; i < count; i++ {
		t := &triple{first, second, "", nil}
		word = m.getThird(t)
		first, second = second, word
		ret[i] = word
		if word == "" {
			return strings.Join(ret, " ")
		}
		if char := word[len(word)-1]; char == '.' {
			if i+WORDS_IN_SENTANCE >= count {
				break
			}
		}
	}
	return strings.Join(ret, " ")
}
开发者ID:nstott,项目名称:gomarkov,代码行数:26,代码来源:gomarkov.go

示例9: setEpidemic

func setEpidemic(index int) {
	for i := 0; i < 4; i++ {
		for {
			rand.Seed(time.Nanoseconds())
			diseasenum := rand.Intn(len(diseases))
			curnum := 0
			found := false
			var diseasename string
			for diseasename, _ = range diseases {
				if curnum == diseasenum {
					for _, dname := range epidemics {
						if diseasename == dname {
							found = true
						}
					}
					break
				}
				curnum++
			}
			if !found {
				epidemics[index] = diseasename
				return
			}
		}
	}
}
开发者ID:ptrr,项目名称:HeatSeeker,代码行数:26,代码来源:main.go

示例10: main

func main() {
	flag.Parse()
	if !CheckFlagsValid() {
		fmt.Printf("Stop training due to invalid flag setting.\n")
		return
	}

	rand.Seed(time.Nanoseconds())

	corpus, err := lda.LoadCorpus(*corpus_file, 2)
	if err != nil {
		fmt.Printf("Error in loading: " + *corpus_file + ", due to " + err.String())
		return
	}

	model := lda.CreateModel(*num_topics, corpus)
	accum_model := lda.NewModel(*num_topics)
	sampler := lda.NewSampler(*topic_prior, *word_prior, model, accum_model)

	for iter := 0; iter < *burn_in_iterations+*accumulate_iterations; iter++ {
		fmt.Printf("Iteration %d ... ", iter)
		if *compute_loglikelihood {
			fmt.Printf("log-likelihood: %f\n", sampler.CorpusLogLikelihood(corpus))
		} else {
			fmt.Printf("\n")
		}
		sampler.CorpusGibbsSampling(corpus, true, iter < *burn_in_iterations)
	}

	if err := accum_model.SaveModel(*model_file); err != nil {
		fmt.Printf("Cannot save model due to " + err.String())
	}

	return
}
开发者ID:cuiweiqiang,项目名称:lda-go,代码行数:35,代码来源:train.go

示例11: makeVector

func makeVector(size int, shape string) vector.Vector {

	var v vector.Vector

	switch shape {

	case "xor":
		for i := 0; i < size; i++ {
			v.Push(&record{0xff & (i ^ 0xab), i})
		}

	case "sorted":
		for i := 0; i < size; i++ {
			v.Push(&record{i, i})
		}

	case "revsorted":
		for i := 0; i < size; i++ {
			v.Push(&record{size - i, i})
		}

	case "random":
		rand.Seed(1)

		for i := 0; i < size; i++ {
			v.Push(&record{rand.Int(), i})
		}

	default:
		panic(shape)
	}

	return v
}
开发者ID:xushiwei,项目名称:timsort,代码行数:34,代码来源:bench_test.go

示例12: main

func main() {
	var ret int
	rand.Seed(12345)

	master := flag.String("m", "", "specify a master!")
	flag.Parse()

	client.Initialize(*master)

	//create file
	fd := client.Open("/newfile.txt", client.O_RDWR|client.O_CREATE)
	if fd < 0 {
		panic("could not create new file")
	}
	//close
	ret = client.Close(fd)
	if ret != client.WIN {
		panic("close failed")
	}

	if client.Delete("/newfile.txt") != 0 {
		panic("Delete failed")
	}

	fd = client.Open("/newfile.txt", client.O_RDWR)
	if fd != -1 {
		panic("open succeeded on deleted file")
	}

	fmt.Printf("\n{{{{{pass}}}}}\n")
	os.Exit(0)
}
开发者ID:alangenfeld,项目名称:cs639,代码行数:32,代码来源:t16.go

示例13: main

func main() {
	q, q2 := new(queue), new(queue)
	q.qs, q2.qs = new(stat), new(stat)
	//	q.in, q.out = make(chan float), make(chan float);
	repeat := 10000
	rand.Seed(2)
	in1, in2, in3, out := make(chan float), make(chan float), make(chan float), make(chan float)
	//	middle1 := make(chan float);
	middle2 := make(chan float)
	middle3 := make(chan float)
	out2 := make(chan float, 100)
	out3, out4 := make(chan float, 100), make(chan float)
	end := make(chan bool)
	generate(in1, repeat)
	generate(in2, repeat)
	generate(in3, repeat)
	combine2(middle3, []<-chan float{in1, in2, in3}, repeat)
	double([]chan<- float{middle2, out2}, middle3, 3*repeat)
	go q.pass(out, middle2, 3*repeat)
	double([]chan<- float{out3, out4}, out, 3*repeat)
	go q2.qs.delay_count(end, out2, out3, 3*repeat)
	for j := 0; j < 3*repeat; j++ {
		<-out4 /* <-out; fmt.Printf("%f\n", <- out)*/
	}
	<-end
	ave := q.qs.interval_sum / float(q.qs.out_num)
	fmt.Printf("Ave %f\nVar %f\n", ave, q.qs.interval_sqsum/float(q.qs.out_num)-ave*ave)
	fmt.Printf("Queued Num Ave %d\n", float(q2.qs.queued_num)/float(q2.qs.out_num))
}
开发者ID:onelittlenightmusic,项目名称:GoChain,代码行数:29,代码来源:qnet.go

示例14: main

func main() {
	deposit := []Element{
		{10, 5},
		{9, 7},
		{8, 3},
		{7, 1},
		{6, 5},
		{5, 2},
		{6, 3},
		{11, 9}}

	args := flag.Args()
	rand.Seed(time.Nanoseconds() % 1e9)
	if len(args) != 1 {
		fmt.Println("Do you miss to specify value for the space in the sack?")
		return
	}

	for _, x := range deposit {
		fmt.Println(x)
	}

	fmt.Println("============================")
	target, _ := strconv.Atoi(args[0])
	tmp := genetic_choose(deposit[0:], target)

	fmt.Println(len(tmp))
	for _, x := range tmp {
		fmt.Println(x)
	}
}
开发者ID:spaghetty,项目名称:spagh_lib,代码行数:31,代码来源:knapsack.go

示例15: main

func main() {
	if len(os.Args) <= 3 {
		log.Fatal("usage: portforward local remote1 remote2 remote3 ...")
	}
	localAddrString := os.Args[1]
	remoteAddrStrings := os.Args[2:]
	localAddr, err := net.ResolveTCPAddr("tcp", localAddrString)
	if localAddr == nil {
		log.Fatalf("net.ResolveTCPAddr failed: %s", err)
	}
	local, err := net.ListenTCP("tcp", localAddr)
	if local == nil {
		log.Fatalf("portforward: %s", err)
	}
	log.Printf("portforward listen on %s", localAddr)

	rand.Seed(time.Nanoseconds())
	for {
		conn, err := local.Accept()
		if conn == nil {
			log.Fatalf("accept failed: %s", err)
		}
		go forward(conn, remoteAddrStrings)
	}
}
开发者ID:phuslu,项目名称:golang-tools,代码行数:25,代码来源:portforward.go


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