Skip to content

Commit 1d45bf2

Browse files
committed
Support Id6 Id8 Id10 Id12
1 parent 3ef2f49 commit 1d45bf2

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

UniqueId.go

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,61 @@ import (
44
"time"
55
)
66

7-
const randMinNumber = 916132832
8-
const randMaxNumber = 56800235583
9-
const randNumber = randMaxNumber - randMinNumber
7+
const randMin6BitNumber = 916132832 // 99999u (100000)
8+
const randMax6BitNumber = 56800235583 // UUUUUU (999999)
9+
const rand6BitNumber = randMax6BitNumber - randMin6BitNumber // 6位
1010

11-
const randMinShortNumber = 3844
12-
const randMaxShortNumber = 238327
13-
const randShortNumber = randMaxShortNumber - randMinShortNumber
11+
const randMin5BitNumber = 14776336 // 9999u (10000)
12+
const randMax5BitNumber = 916132831 // UUUUU (99999)
13+
const rand5BitNumber = randMax5BitNumber - randMin5BitNumber // 5位
14+
15+
const randMin4BitNumber = 238328 // 999u (1000)
16+
const randMax4BitNumber = 14776335 // UUUU (9999)
17+
const rand4BitNumber = randMax4BitNumber - randMin4BitNumber // 4位
18+
19+
const randMin3BitNumber = 3844 // 99u (100)
20+
const randMax3BitNumber = 238327 // UUU (999)
21+
const rand3BitNumber = randMax3BitNumber - randMin3BitNumber // 3位
1422

1523
func UniqueId() string {
16-
buf := EncodeInt(uint64(GlobalRand1.Int63n(randNumber) + randMinNumber))
24+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand6BitNumber) + randMin6BitNumber))
1725
buf = AppendInt(buf, uint64(time.Now().UnixNano()/1000))
18-
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(randNumber)+randMinNumber))
26+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand6BitNumber)+randMin6BitNumber))
1927
return string(cutId(buf, 20))
2028
}
2129

2230
func ShortUniqueId() string {
23-
buf := EncodeInt(uint64(GlobalRand1.Int63n(randShortNumber) + randMinShortNumber))
31+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand3BitNumber) + randMin3BitNumber))
2432
buf = AppendInt(buf, uint64(time.Now().UnixNano()/1000%(86400000000*100)))
25-
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(randShortNumber)+randMinShortNumber))
26-
return string(buf)
33+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand4BitNumber)+randMin4BitNumber))
34+
return string(cutId(buf, 14))
2735
}
2836

29-
func Id() string {
30-
buf := EncodeInt(uint64(GlobalRand1.Int63n(randNumber) + randMinNumber))
31-
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(randNumber)+randMinNumber))
37+
// 约312303亿亿
38+
func Id12() string {
39+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand6BitNumber) + randMin6BitNumber))
40+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand6BitNumber)+randMin6BitNumber))
3241
return string(cutId(buf, 12))
3342
}
3443

35-
func ShortId() string {
36-
buf := EncodeInt(uint64(GlobalRand1.Int63n(randShortNumber) + randMinShortNumber))
37-
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(randShortNumber)+randMinShortNumber))
44+
// 约81亿亿
45+
func Id10() string {
46+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand5BitNumber) + randMin5BitNumber))
47+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand5BitNumber)+randMin5BitNumber))
48+
return string(cutId(buf, 10))
49+
}
50+
51+
// 约 2113536亿
52+
func Id8() string {
53+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand4BitNumber) + randMin4BitNumber))
54+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand4BitNumber)+randMin4BitNumber))
55+
return string(cutId(buf, 8))
56+
}
57+
58+
// 约 550亿
59+
func Id6() string {
60+
buf := EncodeInt(uint64(GlobalRand1.Int63n(rand3BitNumber) + randMin3BitNumber))
61+
buf = AppendInt(buf, uint64(GlobalRand2.Int63n(rand3BitNumber)+randMin3BitNumber))
3862
return string(cutId(buf, 6))
3963
}
4064

UniqueId_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import (
66
"testing"
77
)
88

9+
//func TestId(t *testing.T) {
10+
// for _, nx := range []string{"99u", "UUU", "999u", "UUUU", "9999u", "UUUUU", "99999u", "UUUUUU"} {
11+
// fmt.Println(nx, u.DecodeInt([]byte(nx)))
12+
// }
13+
// for i:=0; i<10; i++ {
14+
// dd := u.ShortUniqueId()
15+
// fmt.Println(dd, len(dd))
16+
// }
17+
//}
18+
919
func TestUniqueId(t *testing.T) {
1020
fmt.Println(u.UniqueId())
1121
uids := map[string]bool{}

0 commit comments

Comments
 (0)