Browse Source

Improve ChoiceMap

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

+ 32
- 21
src/util/arrays.go View File

func ChoiceMap(ary map[string]int) (string, int) { func ChoiceMap(ary map[string]int) (string, int) {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
l := len(ary) l := len(ary)
if l == 0 {
return "", 0
keys := make([]string, 0, l)
for k := range ary {
keys = append(keys, k)
} }
// 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 {
return k, v
}
}
rot++
// fmt.Println(key, val)
}
return key, val
sel := ChoiceStr(keys)
return sel, ary[sel]
} }

// func ChoiceMap(ary map[string]int) (string, int) {
// rand.Seed(time.Now().UnixNano())
// l := len(ary)
// if l == 0 {
// return "", 0
// }
// // 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 {
// return k, v
// }
// }
// rot++
// // fmt.Println(key, val)
// }
// return key, val
// }

Loading…
Cancel
Save