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

dmwin72015 · · 3557 次点击
数字加字母,雪花算法
#7
更多评论
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