Java中隨機數處理工具類

對於訂單號,用戶登錄賬號,我們需要系統生成隨機號段再根據業務規則,生成唯一的隨機數

1、對隨機數的處理,我放在了 RandomUtils.java 中

import java.util.*;

/**
 * @ClassName DateUtil
 * 隨機數處理工具類
 * @Author Lizhou
 * @Date 2019-09-05 12:43:43
 * @Version 1.0
 **/
public final class RandomUtils {

	/**
	 * 用於隨機選的字符和數字
	 */
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/**
	 * 用於隨機選的字符
	 */
	public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	/**
	 * 用於隨機選的數字
	 */
	public static final String NUMBERCHAR = "0123456789";

	private RandomUtils() {
		throw new AssertionError();
	}

	/**
	 * 生成制定範圍內的隨機數字
	 * @param scopeMin 最小數
	 * @param scoeMax 最大數
	 * @return 隨機數
	 */
	public static int integer(int scopeMin, int scoeMax) {
		Random random = new Random();
		return random.nextInt(scoeMax) % (scoeMax - scopeMin + 1) + scopeMin;
	}

	/**
	 * 返回固定長度的隨機數字
	 * @param length 長度
	 * @return 隨機數
	 */
	public static String number(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(NUMBERCHAR.charAt(random.nextInt(NUMBERCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一個定長的隨機字符串(只包含大小寫字母、數字)
	 * @param length 隨機字符串長度
	 * @return 隨機字符串
	 */
	public static String getString(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一個定長的隨機純字母字符串(只包含大小寫字母)
	 * @param length 隨機字符串長度
	 * @return 隨機字符串
	 */
	public static String getMixString(int length) {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
		}
		return sb.toString();
	}

	/**
	 * 返回一個定長的隨機純大寫字母字符串(只包含大小寫字母)
	 * @param length 隨機字符串長度
	 * @return 隨機字符串
	 */
	public static String getLowerString(int length) {
		return getMixString(length).toLowerCase();
	}

	/**
	 * 返回一個定長的隨機純小寫字母字符串(只包含大小寫字母)
	 * @param length 隨機字符串長度
	 * @return 隨機字符串
	 */
	public static String getUpperString(int length) {
		return getMixString(length).toUpperCase();
	}

	/**
	 * 生成一個定長的純0字符串
	 * @param length 字符串長度
	 * @return 純0字符串
	 */
	public static String getZeroString(int length) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < length; i++) {
			sb.append('0');
		}
		return sb.toString();
	}

	/**
	 * 根據數字生成一個定長的字符串,長度不夠前面補0
	 * @param num  數字
	 * @param fixdlenth 字符串長度
	 * @return 定長的字符串
	 */
	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuilder sb = new StringBuilder();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(getZeroString(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("將數字" + num + "轉化爲長度爲" + fixdlenth + "的字符串發生異常!");
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 根據數字生成一個定長的字符串,長度不夠前面補0
	 * @param num 數字
	 * @param fixdlenth 字符串長度
	 * @return 定長的字符串
	 */
	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuilder sb = new StringBuilder();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(getZeroString(fixdlenth - strNum.length()));
		} else {
			throw new RuntimeException("將數字" + num + "轉化爲長度爲" + fixdlenth + "的字符串發生異常!");
		}
		sb.append(strNum);
		return sb.toString();
	}

	/**
	 * 每次生成的len位數都不相同
	 * @param param
	 * @return 定長的數字
	 */
	public static int getNotSimple(int[] param, int len) {
		Random rand = new Random();
		for (int i = param.length; i > 1; i--) {
			int index = rand.nextInt(i);
			int tmp = param[index];
			param[index] = param[i - 1];
			param[i - 1] = tmp;
		}
		int result = 0;
		for (int i = 0; i < len; i++) {
			result = result * 10 + param[i];
		}
		return result;
	}

	/**
	 * 從指定的數組中隨機數組中的某個元素
	 * @param param 指定的數組
	 * @return 隨機元素
	 */
	public static <T> T randomItem(T[] param) {
		int index = integer(0, param.length);
		return param[index];
	}

	/**
	 * 實現一個簡單的字符串乘法
	 * @param str 字符串
	 * @param multiplication 乘法數量
	 * @return
	 */
	@SuppressWarnings("unused")
	private static String strMultiplication(String str, int multiplication) {
		StringBuilder buffer = new StringBuilder();
		for (int i = 0; i < multiplication; i++) {
			buffer.append(str);
		}
		return buffer.toString();
	}

	/**
	 * 返回一個UUID編碼 UUID:通用唯一識別碼 (Universally Unique Identifier)
	 * @return 小寫的UUID
	 */
	public static String uuid() {
		UUID uuid = UUID.randomUUID();
		String s = uuid.toString();
		return s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
	}

	/**
	 * 返回一個UUID編碼 UUID:通用唯一識別碼 (Universally Unique Identifier)
	 * @return 大寫的UUID
	 */
	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String s = uuid.toString();
		String temp = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23)
				+ s.substring(24);
		return temp.toUpperCase();
	}
}

如您在閱讀中發現不足,歡迎留言!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章