The code copy is as follows:
String password = RandomUtil.generateString(10);
The source code is as follows:
The code copy is as follows:
package com.javaniu.core.util;
import java.util.Random;
public class RandomUtil {
public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String NUMBERCHAR = "0123456789";
/**
* Return a fixed length random string (including only upper and lower case letters and numbers)
*
* @param length
* Random string length
* @return Random string
*/
public static String generateString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
}
return sb.toString();
}
/**
* Return a fixed length random pure letter string (including only upper and lower case letters)
*
* @param length
* Random string length
* @return Random string
*/
public static String generateMixString(int length) {
StringBuffer sb = new StringBuffer();
Random random = new Random();
for (int i = 0; i < length; i++) {
sb.append(ALLCHAR.charAt(random.nextInt(LETTERCHAR.length())));
}
return sb.toString();
}
/**
* Return a fixed length random pure capital letter string (including only upper case letters)
*
* @param length
* Random string length
* @return Random string
*/
public static String generateLowerString(int length) {
return generateMixString(length).toLowerCase();
}
/**
* Return a fixed length random pure lowercase string (including only uppercase and lowercase letters)
*
* @param length
* Random string length
* @return Random string
*/
public static String generateUpperString(int length) {
return generateMixString(length).toUpperCase();
}
/**
* Generate a fixed-length pure 0 string
*
* @param length
* string length
* @return Pure 0 string
*/
public static String generateZeroString(int length) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
sb.append('0');
}
return sb.toString();
}
/**
* Generate a fixed-length string based on the number, the length is not enough to be added to the previous one.
*
* @param num
* number
* @param fixdlenth
* string length
* @return Fixed length string
*/
public static String toFixdLengthString(long num, int fixdlenth) {
StringBuffer sb = new StringBuffer();
String strNum = String.valueOf(num);
if (fixdlenth - strNum.length() >= 0) {
sb.append(generateZeroString(fixdlenth - strNum.length()));
} else {
throw new RuntimeException("replace number" + num + "convert to length" + fixdlenth
+ "Exception occurred in the string!");
}
sb.append(strNum);
return sb.toString();
}
/**
* The number of len digits generated each time is different
*
* @param param
* @return Number of fixed length
*/
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;
}
public static void main(String[] args) {
System.out.println("Returns a fixed length random string (contains only uppercase and uppercase letters and numbers):" + generateString(10));
System.out
.println("Returns a fixed length random pure letter string (contains only upper and lower case letters):" + generateMixString(10));
System.out.println("Returns a fixed length random pure uppercase string (including only uppercase and uppercase letters):"
+ generateLowerString(10));
System.out.println("Returns a fixed length random pure lowercase string (including only uppercase and lowercase letters):"
+ generateUpperString(10));
System.out.println("Generate a fixed-length pure 0 string:" + generateZeroString(10));
System.out.println("Create a fixed-length string based on the number, the length is not enough to be added to the previous one:"
+ toFixdLengthString(123, 10));
int[] in = { 1, 2, 3, 4, 5, 6, 7 };
System.out.println("The number of len digits generated each time is different:" + getNotSimple(in, 3));
}
}
There are pictures and truth:
Java Random Character Supplement
Today I saw a Java random character generation demo in zuidaimai, which happened to be used, but I found that it was incomplete, so I reorganized it and shared it with friends in need
The code copy is as follows:
public static void main(String[] args) {
//String s = RandomNum.getRandomNumStr(5);
//System.out.println(s);
System.out.println("generates 5 strings containing 5 characters:");
RandomNum.SuiJiZiFuChuan(5,5);
System.out.println("generates 3 strings containing 6 characters:");
RandomNum.SuiJiZiFuChuan(6,3);
System.out.println("Generate any 1 to 20 strings containing any 1 to 10 characters:");
RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),(int)(10*Math.random()));
System.out.println("Random generation character:");
int i=0;
while(i<(int)(10*Math.random())){
RandomNum.SuiJiZiFuChuan((int)(20*Math.random()),1);
i++;
}
}
public static void SuiJiZiFuChuan(int x,int y){
for(int j=0;j<y;j++){
for(int i=0;i<x;i++){
int a=(int)(100*Math.random()+100*Math.random());
while(true){
if(a>96&a<123)
break;
else
a=(int)(100*Math.random()+100*Math.random());
}
System.out.print((char)a);
}
System.out.println();
}
}
Execution results:
Source: http://www.zuidaima.com/share/1585762703215616.htm