求助一下,生成一个不重复的6位短ID,用作邀请码?

dmwin72015 · · 3579 次点击
有人说,这个如果是相邻的id,生成的太相近,容易被猜出来
#6
更多评论
redis或者mysql自增id,然后映射成六位字符串
#1
` var b58Alphabet = []byte("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ") func demo2() { var b58Alphabet = []byte("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ") t := time.Now() rand.Seed(time.Now().Unix()) m := make(map[string]struct{}) total := 1000000 a := 0 for { id := make([]byte, 6) for i := 0; i < 6; i++ { id[i] = b58Alphabet[rand.Int()%len(b58Alphabet)] } idstr := string(id) // 去除重复 // if _, ok := m[idstr]; ok { // continue // } m[idstr] = struct{}{} a++ if a >= total { break } } result := len(m) fmt.Println("生成", total) fmt.Printf("重复率%f\n", float64(total-result)/float64(total)) fmt.Println("用时", time.Since(t)) } `
#2