Public StaticString[] chars =NewString[] {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s", "T", "U", "V", "w", "X", "Y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; Public StaticString Generateshortuuid () {StringBuffer Shortbuffer=NewStringBuffer (); String UUID= Uuid.randomuuid (). toString (). Replace ("-", "" "); for(inti = 0; I < 8; i++) {String str= Uuid.substring (i * 4, I * 4 + 4); intx = Integer.parseint (str, 16); Shortbuffer.append (Chars[x% 0x3e]); } returnshortbuffer.tostring (); }
The short 8-bit UUID idea actually draws on the microblog short domain name generation way, but its repetition probability is too high, and each time generates 4, needs to select one immediately.
The algorithm uses 62 printable characters, randomly generated 32-bit UUID, because the UUID is 16, so the UUID is divided into 8 groups, each 4 is a group, and then through the modulo 62 operation, the result as an index to remove characters,
Method Two:
PublicString Genrandomnum () {intMaxnum = 36; inti; intCount = 0; Char[] str = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ' }; StringBuffer pwd=NewStringBuffer (""); Random R=NewRandom (); while(Count < 8) {i=Math.Abs (R.nextint (maxnum)); if(I >= 0 && i <str.length) {pwd.append (str[i]); Count++; } } returnpwd.tostring (); }
Java generates 8-bit random invitation code, not duplicates