zoukankan      html  css  js  c++  java
  • java生成随机数

    生成不定长度的随机字符串:

    public class RandomUtils {
        public static final String allChar="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        public static String generateString(){
            StringBuffer sb = new StringBuffer();
            Random random = new Random();
            int x = random.nextInt(47);
            for (int i = 0; i < x; i++) {
                sb.append(allChar.charAt(random.nextInt(allChar.length())));
            }
            return sb.toString();
        }
        
        //test code
        public static void main(String[] args) {
            for (int i = 0; i < 20; i++) {
                System.out.println(generateString());
            }
            
        }
    }

    生成指定长度的字符串:

        public static final String randomString(int length) {
              Random randGen = null;
              char[] numbersAndLetters = null;
                if (length < 1) {
                    return null;
                }
                if (randGen == null) {
                       randGen = new Random();
                       numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz" +
                          "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                        }
                char [] randBuffer = new char[length];
                for (int i=0; i<randBuffer.length; i++) {
                    randBuffer[i] = numbersAndLetters[randGen.nextInt(71)];
                }
                return new String(randBuffer);
        }
  • 相关阅读:
    v-date
    文字在图片上
    v-生命周期
    彭博接口分类
    如何指定vim 的查找是从上往下还是从下往上[z]
    查看linux版本
    git web找不到new project解决方法
    比特币运行原理[z]
    [Z]haproxy+keepalived高可用群集
    blockchain good article
  • 原文地址:https://www.cnblogs.com/hutton/p/4299146.html
Copyright © 2011-2022 走看看