利用Math.random()生成字符串随机码

 1 /**
 2      * 生成字符串随机码
 3      * <p>Title: getRandomString</p>
 4      * <p>Description: </p>
 5      * @param length
 6      * @return
 7      */
 8     public static String getRandomString(int length){
 9          String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
10          Random random=new Random();
11          StringBuffer sb=new StringBuffer();
12          for(int i=0;i<length;i++){
13            int number=random.nextInt(62);
14            sb.append(str.charAt(number));
15          }
16          return sb.toString();
17      }

 

posted @ 2017-11-06 10:59  Monkey_Hi_猴子喂  阅读(339)  评论(0编辑  收藏  举报