package com.xunwang.commonutils;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 随机生成2-3个字的姓名
 */
public class RandomNameUtils {

    public static String generateName() throws UnsupportedEncodingException {

        // 判断名字是两个字还是三个字
        Random random = new Random();
        int num = random.nextInt(4);
        if (num < 3) {
            num = 2;
        }

        // 生成的姓名
        List<String> names = new ArrayList<>();
        String str = "";

        for (int i = 0; i < num; i++) {
            int highPos;
            int lowPos;

            highPos = (176 + Math.abs(random.nextInt(39)));
            lowPos = (161 + Math.abs(random.nextInt(93)));

            byte[] b = new byte[2];
            b[0] = (Integer.valueOf(highPos)).byteValue();
            b[1] = (Integer.valueOf(lowPos)).byteValue();

            str = new String(b, "GBK");

            names.add(str);
        }

        String resultName = String.join("", names);
        return resultName;
    }
}
