gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/myx/scale/confuse.go (about) 1 package scale 2 3 import ( 4 "math/rand" 5 "sync" 6 ) 7 8 func NewConfuseRule(seed int64, n int) (obj *ConfuseRule) { 9 obj = &ConfuseRule{ 10 normalToConfuse: map[uint8]uint8{}, 11 confuseToNormal: map[uint8]uint8{}, 12 } 13 14 arr := make([]string, n) 15 for i := 0; i < n; i++ { 16 arr[i] = tenToAny[i] 17 } 18 rander := rand.New(rand.NewSource(seed)) 19 for i := len(arr) - 1; i > 0; i-- { 20 num := rander.Intn(i + 1) 21 arr[i], arr[num] = arr[num], arr[i] 22 } 23 24 for i := 0; i < n; i++ { 25 normal := tenToAny[i][0] 26 confuse := arr[i][0] 27 28 obj.mutex.Lock() 29 obj.normalToConfuse[normal] = confuse 30 obj.confuseToNormal[confuse] = normal 31 obj.mutex.Unlock() 32 } 33 34 return 35 } 36 37 type ConfuseRule struct { 38 normalToConfuse map[uint8]uint8 39 confuseToNormal map[uint8]uint8 40 mutex sync.RWMutex 41 } 42 43 func (rule *ConfuseRule) NormalToConfuse(normal uint8) uint8 { 44 rule.mutex.RLock() 45 defer rule.mutex.RUnlock() 46 return rule.normalToConfuse[normal] 47 } 48 49 func (rule *ConfuseRule) ConfuseToNormal(normal uint8) uint8 { 50 rule.mutex.RLock() 51 defer rule.mutex.RUnlock() 52 return rule.confuseToNormal[normal] 53 }