JAVA generates no repeat 8-bit random code

Source: Internet
Author: User
Tags printable characters repetition uuid

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,

So the repetition rate is greatly reduced.


After testing, there was no duplication in generating 10 million data, which fully met most of the requirements.

The code is posted for everyone's reference.

public static string[] chars = new string[] {  "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" &NBSP;};p ublic static string generateshortuuid ()  {     stringbuffer shortbuffer = new stringbuffer ();     String uuid = uuid.randomuuid (). toString (). Replace ("-",  "");     for   (int i = 0; i < 8; i++)  {         string str = uuid.substring (I&NBSP;*&NBSP;4,&NBSP;I&NBSP;*&NBSP;4&NBSP;+&NBSP;4);         int x = integer.parseint (str, 16);         shortbuffer.append (chars[x % 0x3e]);     }    return shortbuffer.tostring ();}


JAVA generates no repeat 8-bit random code

Related Article

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.