Java generates 8-bit random invitation code, not duplicates

Source: Internet
Author: User
Tags printable characters uuid

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.