All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.rt.core.util.RandomUtil Maven / Gradle / Ivy
package com.rt.core.util;
import com.json.JSONObject;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
/**
* 随机码生成
*/
class RandomUtil {
public static void main(String[] args) {
System.out.println(RandomUtil.genNum(4));
}
public static double getPercentage(double x, double y) {
NumberFormat numberFormat = NumberFormat.getInstance();
numberFormat.setMaximumFractionDigits(2);
String result = numberFormat.format((x / y) * 100);
return RTUtil.todouble(result);
}
/**
* @see Random
*/
private static Random random = new Random();
/**
* 随机码默认长度
*/
private static int DEFAULT_LENGTH = 25;
/**
* 批量生成
*
* @param size int
* @return codeArray
*/
public static Map createCode(int size, int length) {
Map codeArray = new JSONObject(new LinkedHashMap());
for (int i = 0; i < size; i++) {
codeArray.put(beater(createCode(), length), null);
}
return codeArray;
}
public static String beater(String code, int length) {
StringBuffer s = new StringBuffer(code);
for (int i = 0; i < length - 13; i++) {
insert(s, genNum(s.length()), genEnglish(1));
}
String r = s.toString();
r.replaceAll("0", "A");
r.replaceAll("1", "B");
r.replaceAll("o", "C");
r.replaceAll("i", "D");
r.toUpperCase();
return r;
}
/**
* 创建单个随机码
*
* @return String
*/
public static String createRandomCode() {
return codeBeater(createCode(), DEFAULT_LENGTH);
}
/**
* 创建单个随机码
*
* @return String
*/
public static String createRandomCode(int randomLength) {
return codeBeater(createCode(), randomLength);
}
/**
* 返回单个随机数
*
* @param length
* @return String
*/
public static String createRandomNumber(int length) {
return numberBeater(createCode(), length);
}
/**
* 批量生成
*
* @param black int
* @param digit int
* @param black Map
* @return codeArray
*/
public static JSONObject createRandomNumber(int length, int digit, Map black) {
if (black == null) {
black = new HashMap();
}
JSONObject m = new JSONObject();
for (int i = 0; i < length; ) {
int n = genNum(digit);
if (m.containsValue(n) || black.containsKey(n) || n == 0) {
continue;
} else {
m.put(i, n);
i++;
}
}
return m;
}
/**
* 批量生成
*
* @param size int
* @return codeArray
*/
public static List createRandomCode(int size, int length) {
List codeArray = new ArrayList();
for (int i = 0; i < size; i++) {
codeArray.add(codeBeater(createCode(), length));
}
return codeArray;
}
/**
* 利用当前时间生成序号
*
* @return String
*/
public static String createCode() {
return TimeUtil.getDateToString();
}
/**
* 搅拌器. 将要时间和随机字符,随机拼凑到一起.
*
* @param code
* @param length
* @return randomCode
*/
public static String codeBeater(String code, int length) {
StringBuffer s = new StringBuffer(code);
for (int i = 0; i < length; i++) {
insert(s, genNum(s.length()), genEnglish(1));
}
return s.toString();
}
/**
* 搅拌器. 将要时间和随机数字,随机拼凑到一起.
*
* @param code
* @param length
* @return randomCode
*/
public static String numberBeater(String code, int length) {
String s = code;
for (int i = 0; i < length; i++) {
s = insert(s, genNum(s.length()), genNum(9));
}
return s;
}
/**
* 将insertCode插入到source中的某个位置.
*
* @param source
* @param offset
* @param insertCode
* @return string
*/
public static String insert(StringBuffer source, int offset,
String insertCode) {
return source.insert(offset, insertCode).toString();
}
/**
* 将insertCode插入到source中的某个位置.
*
* @param source
* @param offset
* @param insertCode
* @return string
*/
public static String insert(String source, int offset, int insertCode) {
StringBuffer sb = new StringBuffer(source);
return sb.insert(offset, insertCode).toString();
}
/**
* 获得随机数字
*
* @param digit
* @return randomNum
*/
public static int genNum(int digit) {
return random.nextInt(digit);
}
/**
* 获得英文随机字符.
*
* @param amount
* @return randomENChar
*/
public static String genEnglish(int amount) {
byte abyte0[] = new byte[amount];
for (int j = 0; j < amount; j++) {
abyte0[j] = (byte) (random.nextInt(26) + 97);
}
return new String(abyte0);
}
}