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));
        }
    }
    
  • 相关阅读:
    spark 读取mongodb失败,报executor time out 和GC overhead limit exceeded 异常
    在zepplin 使用spark sql 查询mongodb的数据
    Unable to query from Mongodb from Zeppelin using spark
    spark 与zepplin 版本兼容
    kafka 新旧消费者的区别
    kafka 新生产者发送消息流程
    spark ui acl 不生效的问题分析
    python中if __name__ == '__main__': 的解析
    深入C++的new
    NSSplitView
  • 原文地址:https://www.cnblogs.com/fireinwater/p/6951825.html
Copyright © 2011-2022 走看看