生成随机长度文本

阅读更多

1、

package com.common.common;

import java.util.Random;

public class RandomUtil {
	/**
     * 随机产生几位数字:例:maxLength=3,则结果可能是 012
     */
    public static final int produceNumber(int maxLength){
        Random random = new Random();
        return random.nextInt(maxLength);
    }
     
     
    /**
     * 随机产生区间数字:例:minNumber=1,maxNumber=2,则结果可能是 1、2,包括首尾。
     */
    public static int produceRegionNumber(int minNumber,int maxNumber){
        return minNumber + produceNumber(maxNumber);
    }
 
    /**
     * 随机产生几位字符串:例:maxLength=3,则结果可能是 aAz
     * @param maxLength 传入数必须是正数。
     */
    public static String produceString(int maxLength){
        String source = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return doProduce(maxLength, source);
    }
     
    /**
     * 随机产生随机数字+字母:例:maxLength=3,则结果可能是 1Az
     * @param maxLength 传入数必须是正数。
     */
    public static String produceStringAndNumber(int maxLength){
        String source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        return doProduce(maxLength, source);
    }
 
    /**
     * 自定义随机产生结果
     */
    public static String produceResultByCustom(String customString,int maxLength){
        return doProduce(maxLength, customString);
    }
     
    /**
     * 生产结果
     */
    private static String doProduce(int maxLength, String source) {
        StringBuffer sb = new StringBuffer(100);
        for (int i = 0; i < maxLength; i++) {
            final int number =  produceNumber(source.length());
            sb.append(source.charAt(number));
        }
        return sb.toString();
    }
     
    public static void main(String[] args) {
//        System.out.println(RandomUtil.produceNumber(3));
//        System.out.println(RandomUtil.produceRegionNumber(1,2));
//        System.out.println(RandomUtil.produceString(3));        
//        System.out.println(RandomUtil.produceResultByCustom("xushengxiang.", 8));
        System.out.println("AG"+RandomUtil.produceStringAndNumber(8));
 
    }
}

2、

import java.util.Random;

public class RandomUtil {
	public static String[] strs = {"1","2","3","4","5","6","7","8","9","0"
		,"a","b","c","d","e","f","g","g","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","x",
		"A","B","C","D","E","F","G","G","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","X"
	};
	public static int len = strs.length;
	public static String getRandom(int n){
		StringBuffer aa = new StringBuffer("");
		Random ran = new Random();
		for(int i = 0;i 
  

  。。。

 

 

你可能感兴趣的:(生成随机长度文本)