博主首页

生成一个字母数字组合的n位随机码、随机数、随机字符串

package com.cms.util;
/**
 * 生成一个字母数字组合的n位随机码
 * @author abc
 *
 */
public class CodeUtil {
    
      //
     private final static String[] as ={"0","1","2","3","4","5","6","7","8","9",
                                        "a","b","c","d","e","f","g","h","i","j",
                                        "k","L","m","n","p","q","r","s","t","u",
                                        "y","z","A","B","C","D","E","F","G","H",
                                        "I","J","K","L","M","N","P","Q","R","S",
                                        "T","U","Y","Z","6","6","6","6","6","6"};
     
    //获取一个50(0-49)以内的随机数
     public static int getNumber(){
        return (int)(Math.random()*50);
     }
     
     /**
      * 返回一个字母数字组合的n位随机码
      * @param args
      */
     public static String getCode(int s){
         String code="";
         for(int i=0;i<s;i++){
             code+=as[getNumber()];         
         }
         return code;
     }
     public static void main(String[] args) {
         for(int i=0;i<20;i++){
             System.out.println(getCode(4));     
         }
    }
}

验证码、工具类、随机数

posted @ 2019-05-05 15:36  笑~笑  阅读(265)  评论(0编辑  收藏  举报