public class Test94 { public static void main(String[] args) { String str = UUID.randomUUID().toString().replace("-", ""); String s = str.substring(str.length()-20); System.out.println(s); } }
我的思路是:本机 MAC 地址 + 本机硬盘系统分区序列号(或其他硬件相关的信息) + 本地时间信息组成字符串,计算CRC 校验码(或其他特征码算法) + 若干位随机数这样时间信息不会被显式地表现出来,同时引入随机数,以保证不会重复。
20位数字 ,uuid生成的有字母吧
我认为一个唯一UUID码去掉所有的字母之后,就不能保证不和其他的UUID去除字母产生的数字串重复。
好像uuid重复的概率已经非常非常低le。已经可以忽略了吧
UUID是很好的选择,自己用时间来做重复的概率也很低。
简单一点的算法:public class MyUUID { private static List<String> list = new ArrayList<String>(); /** * @param args */ public static void main(String[] args) { for(int i = 0; i < 100; i ++){ System.out.println(produceUID(20)); } }
/** * 生成一个长度为bits的随机数字字串 * @param bits * @return */ public static String produceUID(int bits){ StringBuffer sb = new StringBuffer(""); for(int i = 0; i < bits; i ++){ sb.append((int)(Math.random()*10)); } String tmp = sb.toString(); if(list.contains(tmp)){ produceUID(bits); }else{ list.add(tmp);
如果想作为一个问题讨论的话,可以参考uuid
public class Test94 {
public static void main(String[] args) {
String str = UUID.randomUUID().toString().replace("-", "");
String s = str.substring(str.length()-20);
System.out.println(s);
}
}
我认为一个唯一UUID码去掉所有的字母之后,就不能保证不和其他的UUID去除字母产生的数字串重复。
private static List<String> list = new ArrayList<String>();
/**
* @param args
*/
public static void main(String[] args) {
for(int i = 0; i < 100; i ++){
System.out.println(produceUID(20));
}
}
/**
* 生成一个长度为bits的随机数字字串
* @param bits
* @return
*/
public static String produceUID(int bits){
StringBuffer sb = new StringBuffer("");
for(int i = 0; i < bits; i ++){
sb.append((int)(Math.random()*10));
}
String tmp = sb.toString();
if(list.contains(tmp)){
produceUID(bits);
}else{
list.add(tmp);
return tmp;
}
return "-1";
}
}生成10个随机的
90130417643910346803
86123279693240382128
25039670080914304497
64583945817217832249
31388568717410404863
30058487792224501668
03353877837227640623
85442941173425645202
69811288503133419608
22117116963872198734
* 该方法用来产生一个20位的String唯一标记
* @return
*/
public static String getKey() {
StringBuffer uid = new StringBuffer(20); //get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int)(currentTimeMillis & -1L), 8)); // get the internet address
uid.append(midValueStatic); //get the random number
uid.append(toHex(getRandom(), 8)); return uid.toString();
} private static String toHex(int value, int length) {
char hexDigits[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F' };
StringBuffer buffer = new StringBuffer(length);
int shift = length - 1 << 2;
for (int i = -1; ++i < length; ) {
buffer.append(hexDigits[value >> shift & 0xf]);
value <<= 4;
} return buffer.toString();
}
* 该方法用来产生一个20位的String唯一标记
* @return
*/
public static String getKey() {
StringBuffer uid = new StringBuffer(20); //get the system time
long currentTimeMillis = System.currentTimeMillis();
uid.append(toHex((int)(currentTimeMillis & -1L), 8)); // get the internet address
uid.append(midValueStatic); //get the random number
uid.append(toHex(getRandom(), 8)); return uid.toString();
} private static String toHex(int value, int length) {
char hexDigits[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
'E', 'F' }; //要是纯数字的话,把字母删除掉
StringBuffer buffer = new StringBuffer(length);
int shift = length - 1 < < 2;
for (int i = -1; ++i < length; ) {
buffer.append(hexDigits[value >> shift & 0xf]);
value < <= 4;
} return buffer.toString();
}