com.majiaxueyuan.util.RandomUtil Maven / Gradle / Ivy
The newest version!
package com.majiaxueyuan.util;
import java.util.Random;
public class RandomUtil {
/**
* RANDOM
*/
private final static int RANDOM_BASE = 10;
/**
* getRandom with Length
*
* @param length length
* @return Random
*/
public static String getRandomStr(int length) {
Random random = new Random();
String randStr = "";
for (int i = 0; i < length; i++) {
String randItem = String.valueOf(random.nextInt(RANDOM_BASE));
randStr += randItem;
}
return randStr;
}
/**
* get Code with num and char with in length
*
* @param len length
* @return Random
*/
public static String randomCharAndNumCode(int len) {
char[] identifyStr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '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' };
int min = 0;
int maxnum = identifyStr.length;
String codeStr = "";
for (int i = 0; i < len; i++) {
int num = (int) ((maxnum - min) * Math.random() + min);
codeStr += identifyStr[num];
}
return codeStr;
}
/**
* get Code only with char with in length
*
* @param len length
* @return Random
*/
public static String randomCharCode(int len) {
char[] identifyStr = { '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' };
int min = 0;
int maxnum = identifyStr.length;
String codeStr = "";
for (int i = 0; i < len; i++) {
int num = (int) ((maxnum - min) * Math.random() + min);
codeStr += identifyStr[num];
}
return codeStr;
}
}