随机工具类

以下只是一些简单的随机抽取不同数据类型的随机抽取,看懂其原理还可以推演一些别的方法。比如抽奖,从固定的集合中抽取随机,可以参考随机抽取下标的方法去实现。

package test.random;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Random;

/**
 * 生成随机数
 * 方法1: Math.random()方法,可以随机生产0-1之间的一个double,可以乘以一定的数改变随机范围并转成整型.
 *      优点:随机的起止范围比较好自定义
 * 方法2:在java.util这个包里面提供了一个Random的类,新建一个Random对象产生随机Int、随机float、随机double,随机long
 *      优点:数据类型丰富,专业封装类。
 *      缺点:起始值是从0开始的;返回结果可以含起始值但不会含有结束值,如需包含结束值,在传参end上加一个单位即可
 *      
 * @author yulisao
 * @createDate 2020-1-13
 * @description
 */
public class RandomUtil {
	
	private static Random random = new Random();
	
	private static char[] mix_str = { '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',
		'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' };
	private static char[] mumbers_str = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	private static char[] letters_str = { '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'};
	
	/**
	 * 生成begin~end随机整数
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @return
	 */
	public static int getRandomInt(int begin, int end) {
		if (end<begin) {
			throw new IllegalArgumentException("起始值不可大于结束值");
		}
		return (int)(Math.random()*(end-begin+1))+begin;
	}
	
	/**
	 * 生成0~end随机整数
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param end 随机范围结束值
	 * @return
	 */
	public static int getRandomInt2(int end) {
		return new Random().nextInt(end);
	}
	
	/**
	 * 生成范围内double型数据
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @return
	 */
	public static double getRandomDouble(double begin, double end) {
		if (end<begin) {
			throw new IllegalArgumentException("起始值不可大于结束值");
		}
		return random.nextDouble()*(end-begin)+begin;
	}
	
	/**
	 * 生成范围内double型数据(结果保留两位小数)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @return
	 */
	public static double getRandomDouble2(double begin, double end) {
		if (end<begin) {
			throw new IllegalArgumentException("起始值不可大于结束值");
		}
		return (double) Math.round((random.nextDouble()*(end-begin)+begin) * 100) / 100; // or Math.floor(n*100+0.5)/100
	}
	
	/**
	 * 生成范围内double型数据(自定义保留小数位)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @return
	 */
	public static double getRandomDouble(double begin, double end, int scale) {
		if (end<begin||scale<0) {
			throw new IllegalArgumentException("起始值不可大于结束值或精度小于0");
		}
		BigDecimal b = new BigDecimal(random.nextDouble()*(end-begin)+begin);
		return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();   
	}
	
	/**
	 * 随机生产boolean值
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @return
	 */
	public static boolean getRandomBoolean() {
		return random.nextBoolean();
	}
	
	/**
	 * 随机生成long
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @return
	 */
	public static Long getRandomLong() {
		return random.nextLong();
	}
	
	/**
	 * 随机生成long(仅含正数)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @return
	 */
	public static Long getRandomLongPositive() {
		return Math.abs(random.nextLong());
	}
	
	/**
	 * 使用单个 long 种子设置此随机数生成器的种子
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param seed
	 */
	public static synchronized void setSeed(long seed) {
		random.setSeed(seed); 
	}
	
	/**
	 * 返回下一个“double类型”的随机数,它是呈高斯(“正常地”)分布的 double 值,其平均值是 0.0,标准偏差是 1.0
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @return
	 */
	public static synchronized double getNextGaussian() {
		return random.nextGaussian(); 
	}
	
	/**
	 * 生成不重复的随机数
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @param counts 生成个数
	 * @return
	 */
	public static ArrayList<Integer> getIntegerArrayList(int begin, int end, int counts) {
		ArrayList<Integer> list = new ArrayList<Integer>();

		for (int i = 0; i < counts; i++) {
			int number = getRandomInt(begin, end);
			if (!list.contains(number)) {
				list.add(number);
			} else {
				i--; //有重复随机数时防止造成空位
			}
		}
		
		return list;
	}
	
	/**
	 * 生成不重复的随机数
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param begin 随机范围起始值
	 * @param end 随机范围结束值
	 * @param counts 生成个数
	 * @return
	 */
	public static ArrayList<Double> getDoubleArrayList(int begin, int end, int counts) {
		ArrayList<Double> list = new ArrayList<Double>();

		for (int i = 0; i < counts; i++) {
			double number = getRandomDouble2(begin, end);
			if (!list.contains(number)) {
				list.add(number);
			} else {
				i--; //有重复随机数时防止造成空位
			}
		}
		
		return list;
	}
	
	/**
	 * 生成随机字符串(纯数字)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param len 字符串长度
	 * @return
	 */
	public static String getRandomStringFromNumbers(int len) {
		/** 原理是循环从 0~35中随机生成待选字符数组的下标,逐个拼接字符 */
		int i; 
		int count = 0; 
		StringBuffer result = new StringBuffer("");
		while (count < len) {
			i = Math.abs(random.nextInt(mumbers_str.length + 1));
			if (i >= 0 && i < mumbers_str.length) {
				result.append(mumbers_str[i]);
				count++;
			}
		}

		return result.toString();
	}
	
	/**
	 * 生成随机字符串(纯字母)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param len 字符串长度
	 * @return
	 */
	public static String getRandomStringFromLetter(int len) {
		/** 原理是循环从 0~35中随机生成待选字符数组的下标,逐个拼接字符 */
		int i; 
		int count = 0; 
		StringBuffer result = new StringBuffer("");
		while (count < len) {
			i = Math.abs(random.nextInt(letters_str.length + 1));
			if (i >= 0 && i < letters_str.length) {
				result.append(letters_str[i]);
				count++;
			}
		}

		return result.toString();
	}
	
	/**
	 * 生成随机字符串(混合字符)
	 *
	 * @author yulisao
	 * @createDate 2020-1-13
	 * @description 
	 * @param len 字符串长度
	 * @return
	 */
	public static String getRandomStringFromMax(int len) {
		/** 原理是循环从 0~35中随机生成待选字符数组的下标,逐个拼接字符 */
		int i; 
		int count = 0; 
		StringBuffer result = new StringBuffer("");
		while (count < len) {
			i = Math.abs(random.nextInt(mix_str.length + 1));
			if (i >= 0 && i < mix_str.length) {
				result.append(mix_str[i]);
				count++;
			}
		}

		return result.toString();
	}
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值