我觉得用UUID生成的字符串就比较合适。import java.util.UUID; public class Test { public static void main(String[] args) { System.out.println(UUID.randomUUID()); } } 输入结果为: 8871a9de-a441-4755-876a-41f9953d1cca生成的是唯一的一个36位ID。决对不会重复。 你可以把其中的“-”去掉。如果位数你认为太多的话,可以从后面开始截取,截取出你最终希望的位数。
不是生成ID类型的 我是说比如生成一个密码是:1d5s35a类似的一个密码
import java.util.UUID; public class Test { public static void main(String[] args) { String str = UUID.randomUUID().toString(); str = str.substring(str.length()-8); System.out.println(str); } }
package test;import java.util.Random;public class RandomPassword { public static char RandomChar() { int randInt; Random rd = new Random(); do{ randInt = rd.nextInt(122); }while(randInt<48||(57<randInt&&randInt<65)||(randInt>90&&randInt<97)); return (char)randInt; } public static void main(String[] args) { Random rand = new Random(); char[] password = new char[rand.nextInt(50)]; for(int i=0;i<password.length;i++) password[i] = RandomChar(); System.out.println("password:"+new String(password)); } }
package com.test;import java.util.Random;public class RandomPasswordTest {
/** * 生成随即密码 * @param pwd_len 生成的密码的总长度 * @return 密码的字符串 */ public static String genRandomNum(int pwd_len) { //35是因为数组是从0开始的,26个字母+10个数字 final int maxNum = 36; int i; //生成的随机数 int count = 0; //生成的密码的长度 char[] str = { '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' }; StringBuffer pwd = new StringBuffer(""); Random r = new Random(); while (count < pwd_len) { //生成随机数,取绝对值,防止生成负数, i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1 if (i >= 0 && i < str.length) { pwd.append(str[i]); count++; } } return pwd.toString(); }
public static void main(String[] args) { System.out.println(genRandomNum(8)); } }
package files; import java.util.*; public class random1 { public static String excute(int length) { StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); StringBuffer sb = new StringBuffer(); Random r = new Random(); int range = buffer.length(); for (int i = 0; i < length; i ++) { sb.append(buffer.charAt(r.nextInt(range))); } return sb.toString(); } public static void main(String args[]) { String res = excute(4); System.out.println(res); }} String res = excute(4); 这一句设置密码的长度
public class Test {
public static void main(String[] args) {
System.out.println(UUID.randomUUID());
}
}
输入结果为:
8871a9de-a441-4755-876a-41f9953d1cca生成的是唯一的一个36位ID。决对不会重复。
你可以把其中的“-”去掉。如果位数你认为太多的话,可以从后面开始截取,截取出你最终希望的位数。
我是说比如生成一个密码是:1d5s35a类似的一个密码
import java.util.UUID;
public class Test {
public static void main(String[] args) {
String str = UUID.randomUUID().toString();
str = str.substring(str.length()-8);
System.out.println(str);
}
}
package test;import java.util.Random;public class RandomPassword {
public static char RandomChar() {
int randInt;
Random rd = new Random();
do{
randInt = rd.nextInt(122);
}while(randInt<48||(57<randInt&&randInt<65)||(randInt>90&&randInt<97));
return (char)randInt;
}
public static void main(String[] args) {
Random rand = new Random();
char[] password = new char[rand.nextInt(50)];
for(int i=0;i<password.length;i++)
password[i] = RandomChar();
System.out.println("password:"+new String(password));
}
}
package com.test;import java.util.Random;public class RandomPasswordTest {
/**
* 生成随即密码
* @param pwd_len 生成的密码的总长度
* @return 密码的字符串
*/
public static String genRandomNum(int pwd_len) {
//35是因为数组是从0开始的,26个字母+10个数字
final int maxNum = 36;
int i; //生成的随机数
int count = 0; //生成的密码的长度
char[] str = { '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' }; StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < pwd_len) {
//生成随机数,取绝对值,防止生成负数, i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1 if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count++;
}
} return pwd.toString();
}
public static void main(String[] args) { System.out.println(genRandomNum(8));
} }
import java.util.*;
public class random1 { public static String excute(int length)
{
StringBuffer buffer = new StringBuffer("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
StringBuffer sb = new StringBuffer();
Random r = new Random();
int range = buffer.length();
for (int i = 0; i < length; i ++)
{
sb.append(buffer.charAt(r.nextInt(range)));
}
return sb.toString();
}
public static void main(String args[])
{
String res = excute(4);
System.out.println(res);
}}
String res = excute(4); 这一句设置密码的长度