本文整理汇总了Golang中rand.Intn函数的典型用法代码示例。如果您正苦于以下问题:Golang Intn函数的具体用法?Golang Intn怎么用?Golang Intn使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Intn函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Turn
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: 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())
}
示例3: mutate
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: main
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: 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)
}
}
示例6: TestRandomSliceAccess
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: hello
// 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: rndString
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: Create
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: rndSlice
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: update
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: Randomize
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: main
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: RandomString
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: BenchmarkUpdateStart
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()
}