码迷,mamicode.com
首页 > 编程语言 > 详细

Java 随机数生成工具RandomUtils

时间:2018-06-29 15:00:40      阅读:1953      评论:0      收藏:0      [点我收藏+]

标签:rip   upper   html   div   intern   version   com   uppercase   大小写   

RandomUtils

/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.</p>
 *  <p> Created on 19941115</p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.alpaca.framework.utils.math;

import java.util.Random;

/**
* @Package:cn.ucaner.common.utils.math   
* @ClassName:RandomUtils   
* @Description:   <p> 随机数工具类</p>
* @Author: - Jason 
* @CreatTime:2017年10月26日 上午10:50:29   
* @Modify By:   
* @ModifyTime:  
* @Modify marker:   
* @version    V1.0
 */
public class RandomUtils {
	
	private static final String ALL_CHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	private static final String LETTER_CHAR = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
	private static final String NUMBER_CHAR = "0123456789";
    
    /**
     * 获取定长的随机数,包含大小写、数字
     * @param length 随机数长度
     * @return
     */
    public static String generateString(int length) { 
        StringBuffer sb = new StringBuffer(); 
        Random random = new Random(); 
        for (int i = 0; i < length; i++) { 
                sb.append(ALL_CHAR.charAt(random.nextInt(ALL_CHAR.length()))); 
        } 
        return sb.toString(); 
    } 
    
    /**
     * 获取定长的随机数,包含大小写字母
     * @param length 随机数长度
     * @return
     */
    public static String generateMixString(int length) { 
        StringBuffer sb = new StringBuffer(); 
        Random random = new Random(); 
        for (int i = 0; i < length; i++) { 
                sb.append(LETTER_CHAR.charAt(random.nextInt(LETTER_CHAR.length()))); 
        } 
        return sb.toString(); 
    } 
    
    /**
     * 获取定长的随机数,只包含小写字母
     * @param length 随机数长度
     * @return
     */
    public static String generateLowerString(int length) { 
        return generateMixString(length).toLowerCase(); 
    } 
    
    /**
     * 获取定长的随机数,只包含大写字母
     * @param length 随机数长度
     * @return
     */
    public static String generateUpperString(int length) { 
        return generateMixString(length).toUpperCase(); 
    } 
    
    /**
     * 获取定长的随机数,只包含数字
     * @param length 随机数长度
     * @return
     */
    public static String generateNumberString(int length){
    	StringBuffer sb = new StringBuffer(); 
        Random random = new Random(); 
        for (int i = 0; i < length; i++) { 
                sb.append(NUMBER_CHAR.charAt(random.nextInt(NUMBER_CHAR.length()))); 
        } 
        return sb.toString(); 
    }
    
    /**
     * For Test by Jason
     */
    public static void main(String[] args) {
		System.out.println("***********Test******************");
		System.out.println("大小写数字:"+generateString(10));
		System.out.println("大小写字母:"+generateMixString(10));
		System.out.println("小写字母:"+generateLowerString(10));
		System.out.println("大写字母:"+generateUpperString(10));
		System.out.println("纯数字:"+generateNumberString(10));
		System.out.println("***********Test******************");
	}
    
}
//Outputs
//***********Test******************
//大小写数字:SsvQTjEbMW
//大小写字母:nJaVyqdmAP
//小写字母:ppljietimr
//大写字母:YDAGERAFCR
//纯数字:8986305301
//***********Test******************

  

Java 随机数生成工具RandomUtils

标签:rip   upper   html   div   intern   version   com   uppercase   大小写   

原文地址:https://www.cnblogs.com/jasonandy/p/9243337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!