zoukankan      html  css  js  c++  java
  • 生成随机的数字和字母组合

    import java.util.Random;
    
    public class StringUtil {
        private static final int Num_WORD = 1;//数字
        private static final int STR_WORD = 2;//字母
        private static final int STR_NUM_WORD = 3;//字母数字
        private static final int MIX_WORD = 4;//字母数字符号
    
        /**
         * 获取随机字符串
         * @param style  输出格式  1纯数字,2纯字符串,3字符数字组合,4字符数字符号组合。
         * @param length 输出长度
         * */
    
        public static String getRandomStr(int style, int length) {
    
             if (style == Num_WORD) {
                    return getNumRandom(length);
                } else if (style == STR_WORD) {
                    return getStrRandom(length);
                } else if (style == STR_NUM_WORD) {
                    return getStrNumRandom(length);
                }else if (style == MIX_WORD) {
                    return getMixRandom(length);
                }else{
    
                     return getMixRandom(length);
    
                 }
         
        }
    
        //纯数字
        private static String getNumRandom(int length) {
            int[] array = new int[length];
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < length; i++) {
                array[i] = (int) (Math.random() * 10);
                str.append(array[i]);
            }
            return str.toString();
        }
    
        //纯字母
        private static String getStrRandom(int length) {
            int[] array = new int[length];
            char[] chars = new char[length];
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < length; i++) {
                while (true) {
                    array[i] = (int) (Math.random() * 1000);
                    if ((array[i] > 64 && array[i] < 91)
                            || (array[i] > 96 && array[i] < 123))
                        break;
                }
                chars[i] = (char) array[i];
                str.append(chars[i]);
            }
            return str.toString();
        }
    
        //字母数字组合
        public static String getStrNumRandom(Integer length) {  
            StringBuilder str = new StringBuilder();
            Random random = new Random();  
            for (int i = 0; i < length; i++) {  
                boolean b = random.nextBoolean();  
                if (b) { // 字符串  
                    int choice = random.nextBoolean() ? 65 : 97; //取得65大写字母还是97小写字母  
                    str.append((char) (choice + random.nextInt(26)));// 取得大写字母  
                } else { // 数字  
                    str.append(random.nextInt(10));  
                }  
            }  
            return str.toString();  
        }  
        
        //字母数字符号组合
        private static String getMixRandom(int length) {
            int[] array = new int[length];
            char[] chars = new char[length];
            StringBuilder str = new StringBuilder();
    
            for (int i = 0; i < length; i++) {
                while (true) {
                    array[i] = (int) (Math.random() * 1000);
                    if (array[i] > 47 && array[i] < 91 || (array[i] > 96 && array[i] < 123))
                        break;
                }
                chars[i] = (char) array[i];
                str.append(chars[i]);
            }
            return str.toString();
        }
        
        public static void main(String[] args) {
            System.out.println(StringUtil.getRandomStr(4, 10));
        }
    }
    
  • 相关阅读:
    《家庭财务总管》升级了(1.0.0.1)
    谁拥有接口?
    VScode调试C++工程
    NVIDIA显卡原生管理查询功能nvidiasmi的部分使用功能
    python版本的两款NVIDIA显卡管理查询工具
    pytorch之网络参数统计 torchstat & torchsummary
    电脑、笔记本、手机维修经验分享网站,专业领域网站
    Python使用pynvml查看GPU信息
    【转载】 Ubuntu下使用VSCode的launch.json及tasks.json编写
    笔记本挑电源适配器吗,是不是电压相同情况下保证功率大于等于原装适配器就可以保证笔记本正常运行???
  • 原文地址:https://www.cnblogs.com/fireinwater/p/6951825.html
Copyright © 2011-2022 走看看