Browse Source

Add ChoiceMap

master
Noëlle Anthony 5 years ago
parent
commit
5d2bc47403
1 changed files with 30 additions and 1 deletions
  1. 30
    1
      src/util/arrays.go

+ 30
- 1
src/util/arrays.go View File

@@ -1,7 +1,7 @@
package util

import (
// "fmt"
"fmt"
"math/rand"
"time"
)
@@ -13,6 +13,10 @@ import (
// return ary[rand.Intn(l)]
// }

func Debug(msg ...interface{}) {
fmt.Println(msg)
}

func ChoiceInt(ary []int) int {
rand.Seed(time.Now().UnixNano())
l := len(ary)
@@ -26,3 +30,28 @@ func ChoiceStr(ary []string) string {
// fmt.Printf("Selected %s (element %d)\n", ary[r], r)
return ary[r]
}

func ChoiceMap(ary map[string]int) (string, int) {
rand.Seed(time.Now().UnixNano())
l := len(ary)
// fmt.Println(l)
key, val, rot := "", 0, 0
for key == "" {
Debug(rot, key, val)
for k, v := range ary {
Debug(k, v)
// Debug(rot)
r := 0
if rot < 3 {
r = rand.Intn(l)
}
if r == 0 {
key, val = k, v
break
}
}
rot++
// fmt.Println(key, val)
}
return key, val
}

Loading…
Cancel
Save