Java generates random Chinese characters

Source: Internet
Author: User



Method One:



 
 
public static char getRandomChar() {
        return (char) (0x4e00 + (int) (Math.random() * (0x9fa5 - 0x4e00 + 1)));
    }


Method Two: Uncommon Chinese characters


  public static void main(String[] args) {
        RandomHan han = new RandomHan();
        System.out.println(han.getRandomHan());
    }
}
class RandomHan {
    private Random ran=new Random();
    private final static int delta=0x9fa5-0x4e00+1;
    public char getRandomHan() {
        return (char) (0x4e00 + ran.nextInt(delta));
    }


Method three, too troublesome


Random random = new Random();///Random number
String[] rBase = {"0", "1", "2", "3", "4", "5", "6", "7", "8","9", "a", "b", "c", "d", "e", "f" };
// Generate the first area code
int r1 = random.nextInt(3) + 11; //Generate a random number between 11 and 14
String str_r1 = rBase[r1];
// Generate the second area code
int r2;
if (r1 == 13) {
r2 = random.nextInt(7); //Generate a random number between 0 and 7
} else {
r2 = random.nextInt(16); //Generate a random number between 0 and 16
}
String str_r2 = rBase[r2];
// Generate the first bit code
int r3 = random.nextInt(6) + 10; //Generate a random number between 10 and 16
String str_r3 = rBase[r3];
// Generate the second bit code
int r4;
if (r3 == 10) {
r4 = random.nextInt(15) + 1; //Generate a random number between 1 and 16
} else if (r3 == 15) {
r4 = random.nextInt(15); //Generate a random number between 0 and 15
} else {
r4 = random.nextInt(16); //Generate a random number between 0 and 16
}
String str_r4 = rBase[r4];
System.out.println(str_r1 + str_r2 + str_r3 + str_r4);
// Convert the generated internal code to Chinese characters
byte[] bytes = new byte[2];
//Save the generated area code to the first element of the byte array
String str_r12 = str_r1 + str_r2;
int tempLow = Integer.parseInt(str_r12, 16);
bytes[0] = (byte) tempLow;
//Save the generated bitcode to the second element of the byte array
String str_r34 = str_r3 + str_r4;
int tempHigh = Integer.parseInt(str_r34, 16);
bytes[1] = (byte) tempHigh;
String ctmp = new String(bytes,"gb2312"); //Generate Chinese characters according to the byte array
System.out.println("Generate Chinese characters:" + ctmp);
 /**
Chinese to Pinyin
jpinyin download
jpinyin
JPinyin is a Java open source class library for converting Chinese characters to Pinyin. It has made some improvements based on the functions of PinYin4j.
【Main features of JPinyin】
1. Accurate and perfect character library;
Unicode encoding is from 20903 Chinese characters in the range of 4E00-9FA5 and 3007(〇),
JPinyin can convert all Chinese characters except 46 variant characters (the variant characters do not have standard pinyin);
2. The pinyin conversion speed is fast;
After testing, converting Unicode codes from 20902 Chinese characters in the range of 4E00-9FA5, JPinyin took about 100 milliseconds.
3. Multi-pinyin format output support;
JPinyin supports a variety of Pinyin output formats: with phonetic symbols, without phonetic symbols, digital phonetic symbols and pinyin first letter output formats;
4. Common multi-sound character recognition;
JPinyin supports the recognition of common polyphonic characters, including phrases, idioms, and place names;
5. Simplified and Traditional Chinese conversion
 */
 
example:
import java.io.UnsupportedEncodingException;
import java.util.Random;
import opensource.jpinyin.ChineseHelper;
import opensource.jpinyin.PinyinFormat;
import opensource.jpinyin.PinyinHelper;
public class TestChineseCode {
 
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  try {
   for(int i=0;i<10;i++){
    System.out.print("No.:"+(i+1)+" words:");
    CreatChineseCode();
   }
   
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void CreatChineseCode() throws UnsupportedEncodingException{
  Random random = new Random();
        String[] rBase = {"0", "1", "2", "3", "4", "5", "6", "7", "8",
                           "9", "a", "b", "c", "d", "e", "f" };
        // Generate the first area code
        int r1 = random.nextInt(3) + 11; //Generate a random number between 11 and 14
        String str_r1 = rBase[r1];
        // Generate the second area code
        int r2;
        if (r1 == 13) {
            r2 = random.nextInt(7); //Generate a random number between 0 and 7
        } else {
            r2 = random.nextInt(16); //Generate a random number between 0 and 16
        }
        String str_r2 = rBase[r2];
        // Generate the first bit code
        int r3 = random.nextInt(6) + 10; //Generate a random number between 10 and 16
        String str_r3 = rBase[r3];
        // Generate the second bit code
        int r4;
        if (r3 == 10) {
            r4 = random.nextInt(15) + 1; //Generate a random number between 1 and 16
        } else if (r3 == 15) {
            r4 = random.nextInt(15); //Generate a random number between 0 and 15
        } else {
            r4 = random.nextInt(16); //Generate a random number between 0 and 16
        }
        String str_r4 = rBase[r4];
        System.out.println("area code+bit code="+str_r1 + str_r2 + str_r3 + str_r4);
       
        // Convert the generated internal code to Chinese characters
        byte[] bytes = new byte[2];
        //Save the generated area code to the first element of the byte array
        String str_r12 = str_r1 + str_r2;
        int tempLow = Integer.parseInt(str_r12, 16);
        bytes[0] = (byte) tempLow;
        //Save the generated bitcode to the second element of the byte array
        String str_r34 = str_r3 + str_r4;
        int tempHigh = Integer.parseInt(str_r34, 16);
        bytes[1] = (byte) tempHigh;
        String ctmp = new String(bytes,"gb2312"); //Generate Chinese characters according to the byte array
        System.out.println("Generate Chinese characters:" + ctmp);
       
        //String s="China's capital is Beijing";
        String s="";
        s=ctmp;
        char [] c=ctmp.toCharArray();
        //With phonetic transcription zhōng,guó,de,shǒu,dū,shì,běi,jīng
        System.out.println(PinyinHelper.convertToPinyinString(s,",",PinyinFormat.WITH_TONE_MARK));
     //Replace the phonetic symbols with numbers zhong1,guo2,de5,shou3,du1,shi4,bei3,jing1
     System.out.println(PinyinHelper.convertToPinyinString(s,",",PinyinFormat.WITH_TONE_NUMBER));
      
      //Without phonetic symbols zhong,guo,de,shou,du,shi,bei,jing
       
      System.out.println(PinyinHelper.convertToPinyinString(s, ",", PinyinFormat.WITHOUT_TONE));
      
       
       System.out.println( PinyinHelper.getShortPinyin(s));//Output the first letter of Pinyin zgdsdsbj
        
        
        //System.out.println("Whether it is polyphonic: "+PinyinHelper.hasMultiPinyin(‘好‘));//judgment of polyphonic true
     
      //Judgment true polyphonic
     System.out.println("Is it polyphonic: "+(PinyinHelper.hasMultiPinyin(c[0])==false? "Not":"Yes"));
    
       
      //System.out.println(ChineseHelper.convertToSimplifiedChinese("东"));//Traditional Chinese to Simplified Chinese
       
       
       //System.out.println( ChineseHelper.convertToTraditionalChinese("东"));//Simplified to traditional Chinese
        //System.out.println(ChineseHelper.isTraditionalChinese(‘哈‘));//Determine whether it is traditional characters false
    
        
    System.out.println("Whether it is a traditional Chinese character:"+((ChineseHelper.isTraditionalChinese(c[0])==false)? "Not":"Yes"));//Determine whether it is a traditional Chinese character false
 }
}





Java generates random Chinese characters


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.