예제 #1
0
func (self *RandomBot) Turn(comm agent.Comm) {
	defer func() { self.time += 1 }()

	if self.Time()%250 == 0 {
		x := pseudo_rand.Intn(2) - 1
		y := pseudo_rand.Intn(2) - 1
		comm.Move(x, y)
		self.log("info", "moved", x, y)
	}

	if self.Id() == 8 && self.Time() == 500 {
		self.send.Send([]byte("Hello there Number 1."), 1)
	}

	self.hello.Run(comm)
	self.route.Run(self.hello.Neighbors(), comm)
	m := self.send.Run(self.route.Routes(), comm)

	if m != nil {
		self.log("info", self.Time(), "got a message", string([]byte(m.Body())))
	}
	if self.Time()%100 == 9 {
		//         self.log("info", self.Time(), "neighbors", self.hello.Neighbors())

		//         self.log("info", self.Time(), "reachable", self.route.Reachable())
		//         s := fmt.Sprintf("\nRoute Table (%v):\n", self.agent.Id())
		//         for i := uint32(1); i <= 8; i++ {
		//             if route, has := self.routes[i]; has {
		//                 s += fmt.Sprint(route, "\n")
		//             }
		//         }
		//         self.log("info", s)
	}
}
예제 #2
0
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())
}
예제 #3
0
func mutate(src string) (mutated string) {
	charpos := rand.Intn(len(src))
	temp := []byte(src)
	temp[charpos] = uint8(int(src[charpos]) + (rand.Intn(3) - 1))
	mutated = string(temp)
	return
}
예제 #4
0
파일: client.go 프로젝트: hlubek/appchilada
func main() {
	flag.Parse()

	ip := net.ParseIP(*address)
	socket, err := net.DialUDP("udp4", nil, &net.UDPAddr{
		IP:   ip,
		Port: *port,
	})
	if err != nil {
		panic(err.String())
	}
	defer socket.Close()

	for {
		label := randomLabels[rand.Intn(len(randomLabels))]
		var message string
		if rand.Intn(2) < 1 {
			message = `{"type":` + strconv.Itoa(appchilada.EventTypeCount) + `,"name":"` + label + `","value":` + strconv.Itoa(rand.Intn(5)+1) + `}`
		} else {
			message = `{"type":` + strconv.Itoa(appchilada.EventTypeTiming) + `,"name":"` + label + `","value":` + strconv.Itoa(rand.Intn(1000)+10) + `}`
		}
		println(message)
		if _, err := socket.Write([]byte(message)); err != nil {
			println(err.String())
			break
		}
		time.Sleep(int64(rand.Intn(10) * 1e7))
	}
}
예제 #5
0
파일: wuserver.go 프로젝트: rjsmith/zguide
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)
	}
}
예제 #6
0
func TestRandomSliceAccess(t *testing.T) {
	for _, s := range testStrings {
		if len(s) == 0 || s[0] == '\x80' { // the bad-UTF-8 string fools this simple test
			continue
		}
		runes := []int(s)
		str := NewString(s)
		if str.RuneCount() != len(runes) {
			t.Error("%s: expected %d runes; got %d", s, len(runes), str.RuneCount())
			break
		}
		for k := 0; k < randCount; k++ {
			i := rand.Intn(len(runes))
			j := rand.Intn(len(runes) + 1)
			if i > j { // include empty strings
				continue
			}
			expect := string(runes[i:j])
			got := str.Slice(i, j)
			if got != expect {
				t.Errorf("%s[%d:%d]: expected %q got %q", s, i, j, expect, got)
			}
		}
	}
}
예제 #7
0
파일: ping.go 프로젝트: bvtsang/patches
// No case-insensitivity for regexp :(
func hello(b *bot.BotState) {
	matched, err := regexp.MatchString("^hi|hello|hey|heyo|Hi|Hello|Hey|Heyo "+b.BotNick, b.Event.Msg)
	if matched && err == nil {
		greeting := [4]string{"Hi", "Hello", "Hey", "Heyo"}
		punctuation := [2]string{".", "!"}
		b.Say(greeting[rand.Intn(4)] + ", " + b.Event.Nick + punctuation[rand.Intn(2)])
	}
}
예제 #8
0
파일: bt.go 프로젝트: taysom/tau
func rndString() string {
	n := rand.Intn(7) + 5
	s := make([]byte, n)
	for j := 0; j < n; j++ {
		s[j] = byte('a' + rand.Intn(26))
	}
	return string(s)
}
예제 #9
0
func (nv TintValue) Create() string {
	val := rand.Intn(0x7F)
	signstr := ""
	if val != 0 && rand.Intn(10) < 5 {
		signstr = "-"
	}
	return fmt.Sprintf("%s%d", signstr, val)
}
예제 #10
0
파일: _.go 프로젝트: taysom/tau
func rndSlice() []byte {
	n := rand.Intn(7) + 5
	s := make([]byte, n)
	for j := 0; j < n; j++ {
		s[j] = byte('a' + rand.Intn(26))
	}
	return s
}
예제 #11
0
파일: main.go 프로젝트: ptrr/HeatSeeker
func update() {
	if (go2d.GetTicks() - ticker) >= 100 {
		for i := 0; i < len(humans); i++ {
			if humans[i].x >= 500 && humans[i].x <= 600 && hatchOpen {
				humans[i].y += 20
				if humans[i].y >= SCREEN_HEIGHT {
					hatchOpen = false
					currentDiseases = make([]*Disease, 0)
					removeHuman(i)
					NewHuman()
					break
				}
				continue
			}
			if humans[i].x >= 150 && humans[i].x <= 230 {
				temp_head = 34 + rand.Intn(7)
				temp_upper = 34 + rand.Intn(7)
				temp_lower = 34 + rand.Intn(7)
			}
			if humans[i].x >= 320 && humans[i].x <= 400 {
				country := countries[rand.Intn(len(countries))]
				if country != nil {
					currentCountry = country
					setDiseases()
				}
			}
			if humans[i].x >= 500 && humans[i].x <= 600 {
				if len(currentDiseases) > 0 {
					for _, dis := range currentDiseases {
						for _, epi := range epidemics {
							if dis.name == epi {
								hatchOpen = true
								//goto is bad? http://kerneltrap.org/node/553/2131
								goto thebatmobile
							}
						}
					}
				thebatmobile:
				}
			}

			humans[i].x += 10

			if humans[i].frame+1 < len(humans[i].frames) {
				humans[i].frame++
			} else {
				humans[i].frame = 0
			}

			if humans[i].x >= SCREEN_WIDTH {
				removeHuman(i)
				NewHuman()
				break
			}
		}
		ticker = go2d.GetTicks()
	}
}
예제 #12
0
func (g *GAOrderedIntGenome) Randomize() {
	l := len(g.Gene)
	for i := 0; i < l; i++ {
		x := rand.Intn(l)
		y := rand.Intn(l)
		g.Gene[x], g.Gene[y] = g.Gene[y], g.Gene[x]
	}
	g.Reset()
}
예제 #13
0
func main() {
	flag.Parse()
	runtime.GOMAXPROCS(*nCPU) // Set number of OS threads to use.

	// Generate tasks phase: enqueues random ligands into the channel.
	ligands := make(chan []byte, 1024)
	go func() {
		for i := 0; i < *nLigands; i++ {
			len := rand.Intn(*maxLigand) + 1
			l := make([]byte, len)
			for j := 0; j < len; j++ {
				l[j] = byte('a' + rand.Intn(26))
			}
			ligands <- l
		}
		// Close the channel
		// so that the map phase will be able decide when to stop.
		close(ligands)
	}()

	// Map phase: consume tasks from the ligands channels,
	// compute the score and send results into the pairs channel.
	pairs := make(chan Pair, 1024)
	for i := 0; i < *nCPU; i++ {
		go func() {
			p := []byte(*protein)
			for l := range ligands {
				pairs <- Pair{score(l, p), l}
			}
		}()
	}

	// Reduce phase.
	// 1. Collect results from the pairs channel
	// into the sorted array.
	sorted := make(PairArray, *nLigands)
	for i := 0; i < *nLigands; i++ {
		sorted[i] = <-pairs
	}
	// 2. Sort the results based on scores.
	sort.Sort(&sorted)
	// 3. Reduce the sorted array into the results array:
	// merge ligands with equal scores.
	var results PairArray
	for i := 0; i < len(sorted); {
		s := sorted[i].score
		var l []byte
		for ; i < len(sorted) && s == sorted[i].score; i++ {
			l = append(append(l, sorted[i].ligand...), ' ')
		}
		results = append(results, Pair{s, l})
	}

	// Output ligands with the highest score.
	fmt.Printf("maximal score is %d, achieved by ligands %s\n",
		results[0].score, string(results[0].ligand))
}
예제 #14
0
func RandomString(alpha string, min int, max int) string {
	alphabet := utf8.NewString(alpha)
	runes := make([]int, rand.Intn(max-(min-1))+min)
	for i, _ := range runes {
		p := rand.Intn(alphabet.RuneCount())
		runes[i] = alphabet.At(p)
	}
	return string(runes)
}
예제 #15
0
func BenchmarkUpdateStart(b *testing.B) {
	d := NewDsl(0, 0, 800, 600)
	for j := 0; j < b.N; j++ {
		x := int32(rand.Intn(800))
		y := int32(rand.Intn(600))

		d.UpdateStart(x, y)
	}
	d.Replan()
}
예제 #16
0
파일: _.go 프로젝트: taysom/tau
func test3() {
	rand.Seed(time.Nanoseconds())
	for i := 0; i < 10; i++ {
		n := rand.Intn(20) + 6
		s := make([]byte, n)
		for j := 0; j < n; j++ {
			s[j] = byte('a') + byte(rand.Intn(26))
		}
		fmt.Println(string(s))
	}
}
예제 #17
0
func BenchmarkUpdateCellOne(b *testing.B) {
	d := NewDsl(0, 0, 800, 600)

	x := int32(rand.Intn(800))
	y := int32(rand.Intn(600))
	cost := rand.Float64() * 100.0

	for j := 0; j < b.N; j++ {
		d.UpdateCell(x, y, cost)
	}
	d.Replan()
}
예제 #18
0
파일: breeder.go 프로젝트: newblue/go-galib
func (breeder *GA2PointBreeder) Breed(a, b GAGenome) (ca, cb GAGenome) {
	if a.Len() != b.Len() {
		panic("Length mismatch in pmx")
	}
	p1 := rand.Intn(a.Len())
	p2 := rand.Intn(b.Len())
	if p1 > p2 {
		p1, p2 = p2, p1
	}
	ca, cb = a.Crossover(b, p1, p2)
	return
}
예제 #19
0
func choosepiece() {
	for {
		i := rand.Intn(len(pieces))
		setpiece(&pieces[i])
		pos = rboard.Min
		pos.X += rand.Intn(NX) * pcsz
		if !collide(draw.Pt(pos.X, pos.Y+pcsz-DY), piece) {
			break
		}
	}
	drawpiece()
	display.FlushImage()
}
예제 #20
0
func makeText(n int) []byte {
	if len(text) >= n {
		return text[:n]
	}
	text = make([]byte, n)
	for i := range text {
		if rand.Intn(30) == 0 {
			text[i] = '\n'
		} else {
			text[i] = byte(rand.Intn(0x7E+1-0x20) + 0x20)
		}
	}
	return text
}
예제 #21
0
func BenchmarkSearch(b *testing.B) {
	b.StopTimer()
	l := New()
	for i := 0; i < 10000; i++ {
		l.Prepend(rand.Intn(10000))
	}
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		x := rand.Intn(10000)
		l.Search(x)
	}

}
예제 #22
0
func BenchmarkUpdateCellRandom(b *testing.B) {
	b.StopTimer()
	d := NewDsl(0, 0, 800, 600)
	for j := 0; j < b.N; j++ {
		x := int32(rand.Intn(800))
		y := int32(rand.Intn(600))
		cost := rand.Float64() * 100.0

		b.StartTimer()
		d.UpdateCell(x, y, cost)
		b.StopTimer()
	}
	d.Replan()
}
예제 #23
0
func gen_line() string {
	op := ""
	switch rand.Intn(3) {
	case 0:
		op = "+"
	case 1:
		op = "-"
	case 2:
		op = "*"
	}
	n1 := strconv.Itoa(rand.Intn(kMaxNum))
	n2 := strconv.Itoa(rand.Intn(kMaxNum))
	if op == "-" && n1 < n2 {
		n1, n2 = n2, n1
	}
	prefix := ""
	s1 := " "
	s2 := " "
	suffix := ""
	switch rand.Intn(200) {
	case 0:
		op = ""
	case 1:
		n1 = ""
	case 2:
		n2 = ""
	case 3:
		s1 = ""
	case 4:
		s2 = ""
	case 5:
		prefix = "   "
	case 6:
		suffix = "   "
	case 7:
		prefix = ""
		n1 = ""
		s1 = ""
		op = ""
		s2 = ""
		n2 = ""
		suffix = ""
	case 8:
		n1 = "foo"
	case 9:
		n2 = "bar"
	}
	return prefix + n1 + s1 + op + s2 + n2 + suffix
}
예제 #24
0
func BenchmarkReplan(b *testing.B) {
	for i := 0; i < b.N; i++ {
		d := NewDsl(0, 0, 256, 512)
		b.StopTimer()
		for j := 0; j < 512; j++ {
			x := int32(rand.Intn(256))
			y := int32(rand.Intn(512))
			cost := rand.Float64() * 100.0

			d.UpdateCell(x, y, cost)
		}
		b.StartTimer()
		d.Replan()
	}
}
예제 #25
0
func randomString(length int) string {
	raw := make([]int, length)
	for i := range raw {
		raw[i] = 97 + rand.Intn(26)
	}
	return string(raw)
}
예제 #26
0
func LongPatterns(alphabet string, min int, max int) []PatternRule {
	rules := make([]PatternRule, rand.Intn(max-(min-1))+min)
	for i, _ := range rules {
		rules[i] = *GenPatternRule(alphabet, 100, 300)
	}
	return rules
}
예제 #27
0
func NewRollOff(w http.ResponseWriter, r *http.Request) {
	rollingUser := ParseUser(r)
	entry := &RollOffEntry{User: rollingUser, Score: rand.Intn(100) + 1}

	rolloff := &RollOff{Id: randomString(20), Open: true}
	rolloff.AddEntry(entry)
	go rolloff.Cycle()

	rolloffs = append(rolloffs, rolloff)

	for elem := room.Users.Front(); elem != nil; elem = elem.Next() {
		go func(e *list.Element) {
			var tName string
			u := e.Value.(*User)
			if u == rollingUser {
				tName = "templates/roll-off/rolling-user.html"
			} else {
				tName = "templates/roll-off/other-users.html"
			}
			t := template.Must(template.ParseFile(tName))
			m := NewMessage("system", "", "system")
			t.Execute(m, entry)
			u.c <- m
		}(elem)
	}
}
예제 #28
0
func EnterRollOff(w http.ResponseWriter, r *http.Request) {
	id := r.URL.Path[len("/roll-off-entry/"):]
	fmt.Println("User wishes to enter rolloff ", id)
	rollingUser := ParseUser(r)
	entry := &RollOffEntry{User: rollingUser, Score: rand.Intn(100) + 1}

	for _, r := range rolloffs {
		fmt.Println("Checking rolloff ", r.Id)
		if r.Id == id {
			r.AddEntry(entry)
			for elem := room.Users.Front(); elem != nil; elem = elem.Next() {
				go func(e *list.Element) {
					var tName string
					u := e.Value.(*User)
					if u == rollingUser {
						tName = "templates/roll-off/user-joins.html"
					} else {
						tName = "templates/roll-off/other-user-joins.html"
					}
					t := template.Must(template.ParseFile(tName))
					m := NewMessage("system", "", "system")
					t.Execute(m, entry)
					u.c <- m
				}(elem)
			}
		}
	}
}
예제 #29
0
파일: gomarkov.go 프로젝트: nstott/gomarkov
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, " ")
}
예제 #30
0
파일: main.go 프로젝트: ptrr/HeatSeeker
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
			}
		}
	}
}