zoukankan      html  css  js  c++  java
  • Java生成指定长度的随机字符串

        /**
         * 获取随机字符串,由数字、大小写字母组成
         * @param bytes:生成的字符串的位数
         * @return
         * @author
         */
        public static String getRandomStr(int bytes){
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < bytes; i++) {
                //随机判断判断该字符是数字还是字母
                String choice = random.nextInt(2) % 2 == 0 ? "char" : "num";
                if ("char".equalsIgnoreCase(choice)) {
                    //随机判断是大写字母还是小写字母
                    int start = random.nextInt(2) % 2 == 0 ? 65 : 97;
                    sb.append((char) (start + random.nextInt(26)));
                } else if ("num".equalsIgnoreCase(choice)) {
                    sb.append(random.nextInt(10));
                }
            }
            return sb.toString();
        }
  • 相关阅读:
    mac pro发热发热发热
    从零开始搭建Vue组件库
    Charles模拟弱网测试
    webpack
    异步加载脚本
    Angular
    JavaScript模板语言
    Node.js
    gulp
    jsonp原理
  • 原文地址:https://www.cnblogs.com/goatherd/p/12201349.html
Copyright © 2011-2022 走看看