您的位置:首页 > 编程语言 > Java开发

JAVA随机生成字符串代码

2017-01-02 19:33 363 查看
package com.banger.ubip.common.utils;  

  

import java.util.Random;  

  

/** 

 * @Author luozx 

 * @Date 2016/12/23 15:59. 

 */  

public class RandomUtil {  

    private static char[] codeSequence1 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',  

                                                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y','z' };  

    private static char[] codeSequence2 = {'0', '1', '3', '4', '5', '6', '7', '8', '9' };  

    private static char[] codeSequence3 = { '!', '@', '#', '$', '%', '&', '*', '_' };  

  

    /** 

     *获取固定位数的随机字符串 

     * @param codeCount // 随机密码字符个数 

     * @return 

     */  

    public static String getRandom(int codeCount){  

        // 生成随机数  

        Random random = new Random();  

        StringBuffer randomCode = new StringBuffer();// randomCode记录随机产生的验证码  

        // 随机产生codeCount个字符的验证码  

        for (int i = 1; i <= codeCount; i++) {  

            String strRand = "";  

            if(i % 3 == 0){  

                strRand = String.valueOf(codeSequence3[random.nextInt(codeSequence3.length)]);  

            }else if(i % 2 == 0){  

                strRand = String.valueOf(codeSequence2[random.nextInt(codeSequence2.length)]);  

            }else{  

                strRand = String.valueOf(codeSequence1[random.nextInt(codeSequence1.length)]);  

            }  

            randomCode.append(strRand); // 将产生的四个随机数组合在一起  

        }  

        return randomCode.toString();  

    }  

  

    public static void main(String[] args) {  

        System.out.println(getRandom(8));  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: