java 随机字符串生成

36位,a-z字符,要求至少跑一天不会有不重复。
private static 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'};
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Set<String> set = new HashSet<String>();
List<String> arr = new ArrayList<String>();
String fileName = "f:\\zyhxx"+".txt";
FileOutputStream fo=new FileOutputStream(fileName);
OutputStreamWriter ow=new OutputStreamWriter(fo,"gbk");
PrintWriter out=new PrintWriter(ow);
int num = 0;
while(true){
StringBuffer buff = new StringBuffer();
for(int i = 0; i < 36; i++){
int m = new Random().nextInt(26);
buff.append(str[m]);
}
String s = buff.toString();
System.out.println((num++)+"\t"+s);
if(arr.contains(s)){
System.out.println(arr.indexOf(s));
System.out.println(s);
break;
}
out.println(s);
arr.add(s);
}
out.close();
}
才100000就重复了

第1个回答  2012-07-12
可以通过生成随机数对26求余
进行与26个字符匹配

就可以生成随机数了。
第2个回答  2012-07-12
这个可以跑一天吗?开玩笑吧!无论怎么随机组也不可能跑出一天的数据量啊
第3个回答  2012-07-15
生成随机数100000次
相似回答