matchnumber.go 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package matchbase
  2. import (
  3. "math/rand"
  4. "sync"
  5. )
  6. const (
  7. min_match_no = 100000
  8. max_match_no = 999999
  9. )
  10. var mn *matchnumber
  11. func getMatchNumber() *matchnumber {
  12. if mn == nil {
  13. mn = new(matchnumber)
  14. mn.run()
  15. }
  16. return mn
  17. }
  18. type matchnumber struct {
  19. lock *sync.RWMutex
  20. matchNumbers []int32
  21. matchIndex int
  22. }
  23. func (mn *matchnumber) run() {
  24. mn.lock = &sync.RWMutex{}
  25. count := max_match_no - min_match_no + 1
  26. mn.matchNumbers = make([]int32, count)
  27. for i := int32(min_match_no); i < int32(max_match_no); i++ {
  28. mn.matchNumbers[i-min_match_no] = i
  29. }
  30. for i := count - 1; i > 1; i-- {
  31. rep := rand.Intn(i)
  32. mn.matchNumbers[i], mn.matchNumbers[rep] = mn.matchNumbers[rep], mn.matchNumbers[i]
  33. }
  34. mn.matchIndex = 0
  35. }
  36. func (mn *matchnumber) getMatchNo() int {
  37. mn.lock.Lock()
  38. defer mn.lock.Unlock()
  39. ret := int(mn.matchNumbers[mn.matchIndex])
  40. mn.matchIndex = (mn.matchIndex + 1) % len(mn.matchNumbers)
  41. return ret
  42. }