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);
        }
  • 相关阅读:
    nxn随机矩阵乘以概率向量依旧是概率向量
    关于飞行器姿态计算
    两矩阵相乘后的秩
    关于矩阵A*b=A*c 中b是否等于c
    5.5节24题
    推论5.2.5
    js中function参数默认值
    陈经纶学校分析数据导出情况
    支付宝申请
    外国javascript资源搜索
  • 原文地址:https://www.cnblogs.com/hutton/p/4299146.html
Copyright © 2011-2022 走看看