Java 随机数工具类(可指定字母或数字生成)


 我在公司写的一个随机数的工具类,比较实用。

 它可以指定字母或数字生成多少位随机数。

 

package com.hanyastar.util.o;

import java.util.Random;

/**    
 * <b>随机类工具</b>
 *    
 * @author Belen      
 * @version 1.0    
 * @since 2013-5-16  
 */    
public abstract class RandomUtils {
	
	public static final char [] LETTER_NUMBER = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	
	public static final char [] LETTER = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 
        'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
	
	public static final char [] NUMBER = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
	
	/** 
     * 按指定大小在<b>26个英文字母</b>中生成随机数。
     *  
     * @param t 
     *         生成的长度,t不能小于1或大于99,否则返回"0"
     * @return 你想要的随机数
     * @created 2013-5-16 下午02:40:05
     * @author Belen
     */  
    public static String getRandomOfLetter(int t) {
       return get(LETTER, t);
    } 
    
	/** 
     * 按指定大小在<b>0-9</b>数字中生成随机数。
     *  
     * @param t 
     *         生成的长度,t不能小于1或大于99,否则返回"0"
     * @return 你想要的随机数
     * @created 2013-5-16 下午02:40:05
     * @author Belen
     */  
    public static String getRandomOfNumber(int t) {
       return get(NUMBER, t);
    }  
    
    /** 
     * 按指定大小在<b>25个英文以及10个数字</b>中生成随机数。
     *  
     * @param t 
     *         生成的长度,t不能小于1或大于99,否则返回"0"
     * @return 你想要的随机数
     * @created 2013-5-16 下午02:40:05
     * @author Belen
     */  
    public static String getRandomOfLetterAndNumber(int t) {
       return get(LETTER_NUMBER, t);
    } 
    
    /** 按指定数组生成数据。*/
	private static String get(char[] c, int t) {
		if(t < 1 || t > 99){
    		return "0";
    	}
		
		final int maxNum = 36;
		int i; // 生成的随机数  
		int count = 0; // 生成的长度  

		StringBuffer sb = new StringBuffer("");
		Random r = new Random();
		while (count < t) {
			// 生成随机数,取绝对值,防止生成负数,  
			i = Math.abs(r.nextInt(maxNum)); // 生成的数最大为36-1  
			if (i >= 0 && i < c.length) {
				sb.append(c[i]);
				count++;
			}
		}
		return sb.toString();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值