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

dmwin72015 · · 3634 次点击
建议先根据字母数字生成全部的邀请码,需要的时候随机导出来一批
#8
更多评论
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