JAVA随机生成字符串代码

  1. package com.banger.ubip.common.utils;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * @Author luozx 
  7.  * @Date 2016/12/23 15:59. 
  8.  */  
  9. public class RandomUtil {  
  10.     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',  
  11.                                                  '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' };  
  12.     private static char[] codeSequence2 = {'0''1''3''4''5''6''7''8''9' };  
  13.     private static char[] codeSequence3 = { '!''@''#''$''%''&''*''_' };  
  14.   
  15.     /** 
  16.      *获取固定位数的随机字符串 
  17.      * @param codeCount // 随机密码字符个数 
  18.      * @return 
  19.      */  
  20.     public static String getRandom(int codeCount){  
  21.         // 生成随机数  
  22.         Random random = new Random();  
  23.         StringBuffer randomCode = new StringBuffer();// randomCode记录随机产生的验证码  
  24.         // 随机产生codeCount个字符的验证码  
  25.         for (int i = 1; i <= codeCount; i++) {  
  26.             String strRand = "";  
  27.             if(i % 3 == 0){  
  28.                 strRand = String.valueOf(codeSequence3[random.nextInt(codeSequence3.length)]);  
  29.             }else if(i % 2 == 0){  
  30.                 strRand = String.valueOf(codeSequence2[random.nextInt(codeSequence2.length)]);  
  31.             }else{  
  32.                 strRand = String.valueOf(codeSequence1[random.nextInt(codeSequence1.length)]);  
  33.             }  
  34.             randomCode.append(strRand); // 将产生的四个随机数组合在一起  
  35.         }  
  36.         return randomCode.toString();  
  37.     }  
  38.   
  39.     public static void main(String[] args) {  
  40.         System.out.println(getRandom(8));  
  41.     }  
  42. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值