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

package util package util


import ( import (
// "fmt"
"fmt"
"math/rand" "math/rand"
"time" "time"
) )
// return ary[rand.Intn(l)] // return ary[rand.Intn(l)]
// } // }


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

func ChoiceInt(ary []int) int { func ChoiceInt(ary []int) int {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
l := len(ary) l := len(ary)
// fmt.Printf("Selected %s (element %d)\n", ary[r], r) // fmt.Printf("Selected %s (element %d)\n", ary[r], r)
return ary[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