@@ -4,37 +4,61 @@ import (
4
4
"time"
5
5
)
6
6
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位
10
10
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位
14
22
15
23
func UniqueId () string {
16
- buf := EncodeInt (uint64 (GlobalRand1 .Int63n (randNumber ) + randMinNumber ))
24
+ buf := EncodeInt (uint64 (GlobalRand1 .Int63n (rand6BitNumber ) + randMin6BitNumber ))
17
25
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 ))
19
27
return string (cutId (buf , 20 ))
20
28
}
21
29
22
30
func ShortUniqueId () string {
23
- buf := EncodeInt (uint64 (GlobalRand1 .Int63n (randShortNumber ) + randMinShortNumber ))
31
+ buf := EncodeInt (uint64 (GlobalRand1 .Int63n (rand3BitNumber ) + randMin3BitNumber ))
24
32
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 ) )
27
35
}
28
36
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 ))
32
41
return string (cutId (buf , 12 ))
33
42
}
34
43
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 ))
38
62
return string (cutId (buf , 6 ))
39
63
}
40
64
0 commit comments