一、
1、生成0-2之间的随机数,包括2
Random rand = new Random();
int randNum = rand.nextInt(3);
2、生成5-26之间的随机数,包括26
int randNum = rand.nextInt(22)+5;
3、java中让随机数产生在一个区间段:
假设你要产生5到10之间的随机数,可以用下面方法:
int Min = 5;
int Max = 10;
int result = Min + (int)(Math.random() * ((Max - Min) + 1));
4、生成六位的随机数:
int rd = (int)((Math.random()*9+1)*100000);
5、Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法
如:
产生一个100以内的整数:int x=(int)(Math.random()*100);
又如:
产生一个1-50之间的随机数:int x=1+(int)(Math.random()*50)
6、
/** * 获取count个随机数 * @param count 随机数个数 * @return */ public static String game(int count){ StringBuffer sb = new StringBuffer(); String str = "0123456789"; Random r = new Random(); for(int i=0;i<count;i++){ int num = r.nextInt(str.length()); sb.append(str.charAt(num)); str = str.replace((str.charAt(num)+""), ""); } return sb.toString(); } public static void main(String[] args) { System.out.println(game(4)); }
7、java 随机生成11位 组合
public static String generate8RateUuid() {
String[] chars = new String[] { "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", "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" };
Random r = new Random();
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < 8; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString()+(r.nextInt(899)+100);
}
二、
1、去掉指定的子字符串:
String filename =worker2.getWorkerScanFront();///upload/workerCard/1542200260139105670.jpeg
String removeStr = "/upload/workerCard";
filename = filename.replace(removeStr, "");