All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.app.common.utils.RandomUtil Maven / Gradle / Ivy

The newest version!
package com.app.common.utils;

import java.util.Random;

public class RandomUtil {
	
	public static Random random = new Random();

	public static String getRandom(int length) {
		StringBuilder ret = new StringBuilder();
		for (int i = 0; i < length; i++) {
			boolean isChar = (random.nextInt(2) % 2 == 0);// 输出字母还是数字
			if (isChar) { // 字符串
				int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
				ret.append((char) (choice + random.nextInt(26)));
			} else { // 数字
				ret.append(Integer.toString(random.nextInt(10)));
			}
		}
		return ret.toString();
	}


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy